How to select a column name with a space in MySQL [duplicate]
Asked Answered
C

6

126

I am working on a project where another developer created a table with column names like 'Business Name'. That is a space between two words. If I run a SELECT statement with 'Business Name' it says there is no column with name 'Business'.

How can I solve this problem?

Carinacarinate answered 7/1, 2013 at 6:9 Comment(1)
I don't have enough reputation to comment properly, so tagged on the end: The solution posted above in the comments: `annoying_table`.`Business Name` did not work for me in mySQL in a where statement. Dropping the back quotes on the table name did work: i.e. annoying_table.`Business Name`Nephritis
U
206

Generally the first step is to not do that in the first place, but if this is already done, then you need to resort to properly quoting your column names:

SELECT `Business Name` FROM annoying_table

Usually these sorts of things are created by people who have used something like Microsoft Access and always use a GUI to do their thing.

Unfamiliar answered 7/1, 2013 at 6:13 Comment(11)
@Unfamiliar If I copy and paste your query it works but if i type single quote from keyboard it does not work. ie. SELECT 'Business Name' FROM annoying_table does not workCarinacarinate
It's because it's not single quotes but rather tick symbols which you can usually find on your keyboard to the left from digit 1.Farseeing
There's three kinds of quotes, single ', double ", and backwards `. In MySQL the first two are equivalent and can be used interchangeably. This is not always the case on other platforms, and Postgres in particular treats them differently. The backticks are used only for database or column name escaping.Unfamiliar
brackets [] didn't work for me. No errors but just didn't displayed it. ` on the other hand worked great.Alexiaalexin
The equivalent of brackets in Sybase or SQL Server is backticks in MySQL.Unfamiliar
What if you need to identify the table as well? IE: annoying_table.Business Name does not seem to work with or without ticks.Interlanguage
@Interlanguage Then you would write it like this: `annoying_table`.`Business Name`.Tamarau
that doesn't work. lm using sql eplorer 4.00 from 2001... `` also [] won't workDuodecimo
SQL Explorer is a front-end client. SQL Server or MySQL are the server software and dialect.Unfamiliar
"... the first step is to not do that in the first place... ". But what about if I want Business Name to appear in reports as it is? i.e. a column name formed by two distinct words.Soulsearching
@Soulsearching Do that in the presentation/application layer, not in the database. The names in the database should be concise, short yet meaningful, and side-step any issues with keyword conflict to avoid having to escape them. You can put whatever you want, in whatever language the user wants in the report.Unfamiliar
N
28

If double quotes does not work , try including the string within square brackets.

For eg:

SELECT "Business Name","Other Name" FROM your_Table

can be changed as

SELECT [Business Name],[Other Name] FROM your_Table

Nighthawk answered 29/9, 2015 at 14:27 Comment(0)
P
26

You need to use backtick instead of single quotes:

Single quote - 'Business Name' - Wrong

Backtick - `Business Name` - Correct

Palaeontography answered 7/12, 2017 at 4:40 Comment(0)
L
5

I got here with an MS Access problem.

Backticks are good for MySQL, but they create weird errors, like "Invalid Query Name: Query1" in MS Access, for MS Access only, use square brackets:

It should look like this

SELECT Customer.[Customer ID], Customer.[Full Name] ...
Lengthways answered 7/3, 2020 at 0:7 Comment(1)
This works with csvsql (csvkit) too.Prissie
S
3

To each his own but the right way to code this is to rename the columns inserting underscore so there are no gaps. This will ensure zero errors when coding. When printing the column names for public display you could search-and-replace to replace the underscore with a space.

Shaff answered 4/8, 2015 at 15:9 Comment(0)
L
-6

I think double quotes works too:

SELECT "Business Name","Other Name" FROM your_Table

But I only tested on SQL Server NOT mySQL in case someone work with MS SQL Server.

Lessard answered 1/9, 2014 at 0:0 Comment(3)
This would always select the string "Business Name", not the content of the column.Debarath
@Debarath Please check again, I used double quote not single quote.... Single quote will result in the string, double quote wont.Lessard
The question was specifically about MySQL and on a MySQL server with standard settings, double quotes and single quotes are equivalent. See this answer for further information why it's just a bad idea to use double quotes in MySQL: https://mcmap.net/q/53880/-when-to-use-single-quotes-double-quotes-and-backticks-in-mysqlDebarath

© 2022 - 2024 — McMap. All rights reserved.