“ORDER BY order DESC"附近的语法错误在 MySQL 中" [英] "Syntax error near "ORDER BY order DESC" in MySQL"

查看:49
本文介绍了“ORDER BY order DESC"附近的语法错误在 MySQL 中"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么,当我尝试按查询执行订单时,我总是收到错误消息,告诉我按 ORDER BY 'order' DESC 检查语法?

Why, when I try to do an order by query, do I always get an error telling me to check the syntax by the ORDER BY 'order' DESC?

这是我的查询:

SELECT * FROM posts ORDER BY order DESC;

我做错了什么?

推荐答案

order 是 SQL 中的保留字;情况无关紧要.当用作标识符时,必须引用.来自 MySQL 保留字 文档:

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:

某些词,例如 SELECT、DELETE 或 BIGINT [或 ORDER] 是保留的,需要特殊处理才能用作标识符,例如表名和列名.

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.

传统的 MySQL 引号:

Traditional MySQL quotes:

SELECT * FROM posts ORDER BY `order` DESC;

正确的 (ANSI) SQL 引号(一些数据库也支持[order]):

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.

这篇关于“ORDER BY order DESC"附近的语法错误在 MySQL 中"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆