选择具有关键字名称的列

时间:2023-01-05 07:40:16

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

现在我想从'test'表中选择我的'AND'列

The query select AND from test; wont work because AND is keyword

查询从测试中选择AND;不会工作,因为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”栏?

6 个解决方案

#1


22  

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

使用反引号来转义关键字。就像你创建表格一样。使用双引号或单引号将其解析为字符串,这就是为什么你得到两个和的记录

select `AND` from test;

#2


3  

  1. Don't create columns with a name that is a keyword.

    不要创建名称是关键字的列。

  2. 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'.

    如果您确实创建了具有此类名称的列,请使用反引号(仅限MySQL)或双引号(SQL标准)来包含名称。我不确定你是否需要打开一些机制让双引号在MySQL中运行。这会创建一个“分隔标识符”。

    SELECT `AND` AS a, "AND" AS b
      FROM test;
    

See also:

也可以看看:

#3


1  

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;

#4


1  

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;

#5


0  

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.

您需要在SELECT语句中使用反引号。

SELECT `AND` FROM test;
-------^---^-----------

See MySQL: Reserved Words

请参阅MySQL:保留字

#6


0  

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

如果你使用选择setement使用“”它选择一个给定的sting,所以你必须给出一个像mssql一样的列名,你必须使用

select [AND] from test;

or in mysql syntex select AND from test;

或者在mysql syntex中选择AND from test;

#1


22  

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

使用反引号来转义关键字。就像你创建表格一样。使用双引号或单引号将其解析为字符串,这就是为什么你得到两个和的记录

select `AND` from test;

#2


3  

  1. Don't create columns with a name that is a keyword.

    不要创建名称是关键字的列。

  2. 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'.

    如果您确实创建了具有此类名称的列,请使用反引号(仅限MySQL)或双引号(SQL标准)来包含名称。我不确定你是否需要打开一些机制让双引号在MySQL中运行。这会创建一个“分隔标识符”。

    SELECT `AND` AS a, "AND" AS b
      FROM test;
    

See also:

也可以看看:

#3


1  

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;

#4


1  

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;

#5


0  

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.

您需要在SELECT语句中使用反引号。

SELECT `AND` FROM test;
-------^---^-----------

See MySQL: Reserved Words

请参阅MySQL:保留字

#6


0  

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

如果你使用选择setement使用“”它选择一个给定的sting,所以你必须给出一个像mssql一样的列名,你必须使用

select [AND] from test;

or in mysql syntex select AND from test;

或者在mysql syntex中选择AND from test;