Stan Blog

一些筆記的記錄

[SQL] SQL 語法內的單引號與雙引號

  1. 單引號、雙引號: 用在 string 文字
  2. 反引號: 用在 table、column identifiers

單引號已被 RDBMS 廣泛的接受。因此避免雙引號,使用單引號是好習慣

// 雙引號包 SQL 語句(包含反引號)
mysql -u root -p -h localhost database -e "SELECT  `users`.* FROM users ORDER BY `users`.`id` DESC LIMIT 1;"

會報錯 ERROR 1064 (42000) at line 1: You have an error in your SQL syntax;

// 單引號包 SQL 語句(包含反引號)
mysql -u root -p -h localhost database -e 'SELECT  `users`.* FROM users ORDER BY `users`.`id` DESC LIMIT 1;'

// 雙引號包 SQL 語句(不包含反引號)
mysql -u root -p -h localhost database -e "SELECT  users.* FROM users ORDER BY users.id DESC LIMIT 1;"

// 單引號包 SQL 語句(包含反引號)
mysql -u root -p -h localhost database -e 'SELECT  users.* FROM users ORDER BY users.id DESC LIMIT 1;'

可正常得到 return 值


Ref:

Comments

comments powered by Disqus