SQL query: Can't order by column called “order”?

后端 未结 7 1752
忘掉有多难
忘掉有多难 2021-01-14 03:27

I am pulling a column in an existing script into my template files, and everything is working great.

The only problem is, that this script has a column called

相关标签:
7条回答
  • 2021-01-14 04:06

    If you are working with Postgres just use "column_name", e.g:

    SELECT "order" FROM table_name WHERE "order" > 10 ORDER BY "order";
    
    0 讨论(0)
  • 2021-01-14 04:07

    AS orderis a SQL keyword, you should escape it properly as an field identifier by using backticks:

    SELECT ... FROM ... ORDER by `order`
    
    0 讨论(0)
  • 2021-01-14 04:09

    Try using backticks:

    SELECT * FROM `categories` WHERE `hide` = 0 ORDER BY `order`
    

    ORDER is a reserved word in SQL. You can use a reserved word as a column name but you must surround it in backticks when referencing it. It's good practice to surround all your column names in backticks so you don't run into this issue.

    0 讨论(0)
  • 2021-01-14 04:15

    From the manual:

    A reserved word can be used as an identifier if you quote it.

    So you can use it like this:

    SELECT * FROM categories WHERE hide = 0 ORDER BY `order`
    
    0 讨论(0)
  • 2021-01-14 04:20

    Worked for me with brackets. SELECT T.* FROM dbo.test AS T ORDER BY [T].[ORDER]

    0 讨论(0)
  • 2021-01-14 04:22

    Try using back ticks around the column name, that should do it.

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