问题描述
我有一个非常简单的查询:
I have this very simple query:
SELECT * FROM "orders" WHERE "printer_id" = 'AC001' AND "sent" = '0'
它抛出以下错误:
#1064 - 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 '"orders" WHERE "printer_id" = 'AC001' AND "sent" = '0' at line 1
从列名中删除"就可以了!如果有帮助,我使用 Medoo (http://medoo.in/) 作为我的数据库框架.
Removing the "" from the column names makes it work! Im using Medoo (http://medoo.in/) as my database framework if that helps.
推荐答案
仅当 SQL_MODE
设置包含 ANSI_QUOTES
时,才使用标识符的双引号.
The double quotes around identifiers are used only if the SQL_MODE
setting includes ANSI_QUOTES
.
默认的标识符引号字符是反引号字符.
The default identifier quote character is the backtick character.
MySQL 返回语法错误,因为它将 "orders"
视为字符串文字,它需要一个标识符(表名).
MySQL is returning a syntax error because it's seeing "orders"
as a string literal, where it's expecting an identifier (a table name).
以下任一方法都可以解决问题:
Either of these should work to get around the problem:
不加引号的标识符:
SELECT * FROM orders WHERE printer_id = 'AC001' AND sent = '0'
使用反引号将标识符括起来
Use backticks to enclose the identifiers
SELECT * FROM `orders` WHERE `printer_id` = 'AC001' AND `sent` = '0'
或者(我不推荐这个,但这是另一种方法......
Or (I dom't recommend this, but it is another approach...
更改 SQL_MODE
的设置以包含 ANSI_QUOTES
选项,并使用双引号作为标识符转义字符,就像在原始语句中一样.
Change the setting of the SQL_MODE
to include ANSI_QUOTES
option, and use the double quotes as the identifier escape character, like in your original statement.
这篇关于MySQL 和引号的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!