MySQL 查詢中的表和列的引號真的有必要嗎?


如果表名或列名是任何的保留字,則需要在 MySQL 查詢中對錶名和列名使用引號。你需在表名和列名周圍使用反引號。語法如下

SELECT *FROM `table` where `where`=condition;

以下是建立不帶引號的、帶有保留字的表的查詢。由於這些是預定義的保留字,因此你將收到錯誤。錯誤如下

mysql> create table table
   -> (
   -> where int
   -> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table
(
   where int
)' at line 1

現在,讓我們在表名和列名周圍加上引號,因為“table”和“where”是保留字。以下是帶引號的查詢

mysql> create table `table`
   -> (
   -> `where` int
   -> );
Query OK, 0 rows affected (0.55 sec)

使用 insert 命令在表中插入記錄。查詢如下

mysql> insert into `table`(`where`) values(1);
Query OK, 1 row affected (0.13 sec)
mysql> insert into `table`(`where`) values(100);
Query OK, 1 row affected (0.26 sec)
mysql> insert into `table`(`where`) values(1000);
Query OK, 1 row affected (0.13 sec)

在 where 的條件幫助下,顯示錶中的特定記錄。查詢如下

mysql> select *from `table` where `where`=100;

以下是輸出

+-------+
| where |
+-------+
| 100   |
+-------+
1 row in set (0.00 sec)

更新於: 2019-07-30

401 次瀏覽

開啟您的職業

完成課程即可獲得認證

開始吧
廣告