Select a column with a keyword name

前端 未结 6 1015
感动是毒
感动是毒 2020-12-03 15:22

I have a table named \'test\'

It contains a column named \'AND\'

For that I use the following queries

insert into test values (\'a\',\'abc\')         


        
相关标签:
6条回答
  • 2020-12-03 15:56
    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'.

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

    See also:

    • What does the SQL Standard say about back-quotes
    • SQL standard to escape column names
    0 讨论(0)
  • 2020-12-03 15:56

    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;
    
    0 讨论(0)
  • 2020-12-03 16:01

    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;
    
    0 讨论(0)
  • 2020-12-03 16:02

    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 `AND` FROM test;
    -------^---^-----------
    

    See MySQL: Reserved Words

    0 讨论(0)
  • 2020-12-03 16:03

    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;
    
    0 讨论(0)
  • 2020-12-03 16:16

    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 [AND] from test;
    

    or in mysql syntex select AND from test;

    0 讨论(0)
提交回复
热议问题