MySQL 选择具有关键字名称的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12400735/
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
Select a column with a keyword name
提问by Fathah Rehman P
I have a table named 'test'
我有一个名为“test”的表
It contains a column named 'AND'
它包含一个名为“AND”的列
For that I use the following queries
为此,我使用以下查询
insert into test values ('a','abc');
insert into test values ('b','def');
Now I want to select my 'AND' column from the 'test' table
现在我想从“测试”表中选择我的“AND”列
The query select AND from test;
wont work because AND is keyword
查询select AND from test;
不起作用,因为 AND 是关键字
I tried
我试过
select "AND" from test;
select 'AND' from test;
Both queries return the wrong results as show below
两个查询都返回错误的结果,如下所示
How can I select my 'AND' column?
如何选择我的“AND”列?
回答by John Woo
use backtick
to escape keyword instead. just like how you created your table. using double quote or single quote will parse it into string
that is why you're getting two records of and
用于backtick
转义关键字。就像您创建表格的方式一样。使用双引号或单引号会将其解析为string
这就是为什么你会得到两条记录and
select `AND` from test;
回答by Jonathan Leffler
Don't create columns with a name that is a keyword.
If you do create a column with such a name, use backticks (MySQL only) or double quotes (SQL standard) to enclose the name. I'm not certain whether you need to switch on some mechanism to have double quotes work in MySQL. This creates a 'delimited identifier'.
SELECT `AND` AS a, "AND" AS b FROM test;
不要创建名称为关键字的列。
如果您确实创建了具有此类名称的列,请使用反引号(仅限 MySQL)或双引号(SQL 标准)将名称括起来。我不确定您是否需要开启某种机制才能在 MySQL 中使用双引号。这将创建一个“分隔标识符”。
SELECT `AND` AS a, "AND" AS b FROM test;
See also:
也可以看看:
回答by JDGuide
Its a SO old question and solved. Read here more.
这是一个老问题并已解决。在这里阅读更多。
But still you can Try this :
但你仍然可以试试这个:
select your_tablename.column_name from table_name;
Your code :-
您的代码:-
select test.AND from test;
回答by Akash KC
If you want to use reserved word as column, you must use backtick around the reserved word column.....
如果要使用保留字作为列,则必须在保留字列周围使用反引号.....
In your case, you have to do like this:
在你的情况下,你必须这样做:
select `AND' from test;
回答by Anant Dabhi
If you use select setement using "" it select a given sting so you have to give a column name like in mssql you have to use
如果您使用 "" 使用 select setement,它会选择一个给定的字符串,因此您必须提供一个列名,就像在 mssql 中一样,您必须使用
select [AND] from test;
or in mysql syntex
select AND
from test;
或在 mysql 语法中AND
从测试中选择;
回答by hims056
From your question:
从你的问题:
CREATE TABLE `test` (
`count` VARCHAR(50) NULL DEFAULT NULL,
`AND` VARCHAR(50) NULL DEFAULT NULL
----^---^ Why do you use `backtick` (`) in CREATE TABLE statement?
)
Same way you need to use backtick
in SELECT
statement.
您需要以相同的方式使用backtick
inSELECT
语句。
SELECT `AND` FROM test;
-------^---^-----------
请参阅MySQL:保留字