Home >Database >Mysql Tutorial >How to SELECT a MySQL Column with a Space in its Name?
Selecting MySQL Columns with Spaces in Their Names
Working with databases often involves dealing with legacy tables, some of which may have column names containing spaces (e.g., 'Business Name'). Directly using such names in a MySQL SELECT
statement will usually fail, resulting in an error because MySQL interprets the space as a separator.
The Solution: Backticks
The correct way to select a column with a space in its name is to enclose the name in backticks (``). For example:
<code class="language-sql">SELECT `Business Name` FROM annoying_table;</code>
The backticks tell MySQL to treat the entire string within them as a single identifier, thus correctly identifying the column.
Best Practice: Avoid Spaces in Column Names
While this solution works, it's strongly recommended to avoid creating tables with column names containing spaces. This prevents potential query errors and improves code readability. Spaces often introduce complications and are generally considered poor database design practice. Using underscores (_
) is a much better alternative (e.g., business_name
).
The above is the detailed content of How to SELECT a MySQL Column with a Space in its Name?. For more information, please follow other related articles on the PHP Chinese website!