Syntax error near “ORDER BY order DESC” in MySQL?

前端 未结 3 2003
栀梦
栀梦 2021-01-14 10:56

Why I try to do an order by query, I always get an error telling me to check the syntax by the ORDER BY \'order\' DESC.... Here\'s my query:

SELECT * FROM p         


        
相关标签:
3条回答
  • 2021-01-14 11:33

    The column name is order which is a keyword. You need to do this:

    SELECT * FROM posts ORDER BY `order` DESC;
    
    0 讨论(0)
  • 2021-01-14 11:57

    Order is reserved keyword.

    Try,

    SELECT * FROM posts ORDER BY `order` DESC;
    
    0 讨论(0)
  • 2021-01-14 11:58

    order is a reserved word in SQL; case does not matter. It must be quoted when used as an identifier. From the MySQL Reserved Words documentation:

    Certain words such as SELECT, DELETE, or BIGINT [or ORDER] are reserved and require special treatment for use as identifiers such as table and column names.

    Traditional MySQL quotes:

    SELECT * FROM posts ORDER BY `order` DESC;
    

    Proper (ANSI) SQL quotes (some databases support [order] as well):

    SELECT * FROM posts ORDER BY "order" DESC;
    

    Although I would consider renaming the column to avoid such confusing issues in the future.

    Happy coding!

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