MySQL 如何在MySQL中选择带空格的列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14190798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select a column name with a space in MySQL
提问by ehp
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'.
我正在开发一个项目,在该项目中,另一位开发人员创建了一个列名如'Business Name'
. 那是两个词之间的空格。如果我运行SELECT
带有“企业名称”的语句,它会显示没有名称为“企业”的列。
How can I solve this problem?
我怎么解决这个问题?
回答by tadman
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.
通常这些类型的东西是由使用过 Microsoft Access 之类的东西并且总是使用 GUI 来做他们的事情的人创建的。
回答by FatherMathew
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
SELECT [Business Name],[Other Name] FROM your_Table
回答by Saurabh
You need to use backtick instead of single quotes:
您需要使用反引号而不是单引号:
Single quote - 'Business Name'
- Wrong
单引号 - 'Business Name'
- 错误
Backtick - `Business Name`
- Correct
反引号 - `Business Name`
- 正确
回答by Valmiki
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.
对于每个人来说,正确的编码方法是重命名插入下划线的列,这样就没有间隙。这将确保编码时零错误。打印列名以供公开显示时,您可以搜索和替换以用空格替换下划线。
回答by kztd
For MS Access, square brackets work and backticks cause all kinds of wierd errors, like "Invalid Query Name: Query1"
对于 MS Access,方括号起作用,反引号会导致各种奇怪的错误,例如“查询名称无效:查询 1”
It should look like this
它应该是这样的
SELECT Customer.[Customer ID], Customer.[Full Name] ...
回答by Vin.X
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.
但我只在 SQL Server 上测试,而不是在 mySQL 上测试,以防有人使用 MS SQL Server。