解決 MySQL 中“ORDER BY order DESC”附近的語法錯誤?


單詞 order 是 MySQL 中的保留順序,並且您已在查詢中使用它。要消除語法錯誤,您需要在 order 周圍使用反引號(` `)。

正確的語法如下 −

select *from yourTableName ORDER BY `order` DESC;

我們首先建立一個表 −

mysql> create table DemoTable
   (
   `order` int
   );
Query OK, 0 rows affected (0.58 sec)

使用 insert 命令在表中插入一些記錄 −

mysql> insert into DemoTable values(89);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values(67);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(90);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(56);
Query OK, 1 row affected (0.13 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

+-------+
| order |
+-------+
| 89    |
| 67    |
| 90    |
| 56    |
+-------+
4 rows in set (0.00 sec)

以下是消除 ORDER BY 附近的語法錯誤的查詢 −

mysql> select *from DemoTable ORDER BY `order` DESC;

輸出

+-------+
| order |
+-------+
| 90    |
| 89    |
| 67    |
| 56    |
+-------+
4 rows in set (0.00 sec)

更新時間:2019 年 7 月 30 日

2K+ 觀看次數

啟動你的職業生涯

完成課程以獲得認證

開始
廣告