我正在学习编写查询的最佳方法。我也明白始终如一的重要性。到目前为止,我没有经过任何真正的思考就随意地使用单引号、双引号和反引号。
例子:
$query = 'INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2)';
同样,在上面的例子中,考虑table、col1、val1等可能是变量。
这个标准是什么?你会怎么做?
我已经在这里看了大约20分钟类似问题的答案,但这个问题似乎没有明确的答案。
我正在学习编写查询的最佳方法。我也明白始终如一的重要性。到目前为止,我没有经过任何真正的思考就随意地使用单引号、双引号和反引号。
例子:
$query = 'INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2)';
同样,在上面的例子中,考虑table、col1、val1等可能是变量。
这个标准是什么?你会怎么做?
我已经在这里看了大约20分钟类似问题的答案,但这个问题似乎没有明确的答案。
当前回答
除了所有这些(解释得很好的)答案之外,我还没有提到以下问题,我经常访问这个问答。
简单地说;MySQL认为您想在它自己的表/列上做数学运算,并将连字符(如“e-mail”)解释为e-mail。
免责声明:因此,对于那些完全不熟悉数据库的人以及可能不理解已经描述的技术术语的人,我认为我将添加这一点作为“供参考”类型的回答。
其他回答
SQL服务器和MySQL, PostgreySQL, Oracle不理解双引号(")。因此,您的查询应该没有双引号("),而应该只使用单引号(')。
Back-trip(')在SQL中是可选的,用于表名、db名和列名。
如果你试图在后台编写查询来调用MySQL,那么你可以使用双引号(")或单引号(')将查询赋值给一个变量,比如:
let query = "select id, name from accounts";
//Or
let query = 'select id, name from accounts';
如果在你的查询中有一个where语句和/或试图插入一个值和/或更新值为字符串,请使用单引号(')对这些值执行如下操作:
let querySelect = "select id, name from accounts where name = 'John'";
let queryUpdate = "update accounts set name = 'John' where id = 8";
let queryInsert = "insert into accounts(name) values('John')";
//Please not that double quotes are only to be used in assigning string to our variable not in the query
//All these below will generate error
let querySelect = 'select id, name from accounts where name = "John"';
let queryUpdate = 'update accounts set name = "John" where id = 8';
let queryInsert = 'insert into accounts(name) values("John")';
//As MySQL or any SQL doesn't understand double quotes("), these all will generate error.
如果你想避免使用双引号(")和单引号(')时的困惑,建议坚持使用单引号('),这将包括反斜杠(),如:
let query = 'select is, name from accounts where name = \'John\'';
双(")或单(')引号的问题出现时,我们必须分配一些值动态和执行一些字符串连接,如:
let query = "select id, name from accounts where name = " + fName + " " + lName;
//This will generate error as it must be like name = 'John Smith' for SQL
//However our statement made it like name = John Smith
//In order to resolve such errors use
let query = "select id, name from accounts where name = '" + fName + " " + lName + "'";
//Or using backslash(\)
let query = 'select id, name from accounts where name = \'' + fName + ' ' + lName + '\'';
如果需要进一步的说明,请在JavaScript中使用引号
在MySQL中,这些符号用于分隔查询',','和()。
或'用于封闭类似字符串的值"26-01-2014 00:00:00"或"26-01-2014 00:00:00"。这些符号只用于字符串,而不是像now、sum或max这样的聚合函数。 ’用于包含表名或列名,例如:select ' column_name ' from ' table_name ' where id='2' (和)简单地包含查询的部分,例如从' table_name '中选择' column_name ' where (id='2' and gender='male')或name='rakesh'。
有时候不用引号是有用的……因为这会突出生成查询的代码中的问题…例如:
x和y的位置应该总是整数…
从x= y=0的表中选择*
是SQL语法错误…有点懒,但很有用……
除了所有这些(解释得很好的)答案之外,我还没有提到以下问题,我经常访问这个问答。
简单地说;MySQL认为您想在它自己的表/列上做数学运算,并将连字符(如“e-mail”)解释为e-mail。
免责声明:因此,对于那些完全不熟悉数据库的人以及可能不理解已经描述的技术术语的人,我认为我将添加这一点作为“供参考”类型的回答。
MySQL中有两种类型的引号:
'用于封装字符串字面量 ’用于包含表名和列名等标识符
然后是“这是一个特例”。根据MySQL服务器的sql_mode,它可以同时用于上述目的之一:
默认情况下,"字符可以用来包含字符串字面量,就像' 在ANSI_QUOTES模式中,“字符可以用来包含标识符,就像'
以下查询将根据SQL模式产生不同的结果(或错误):
SELECT "column" FROM table WHERE foo = "bar"
ANSI_QUOTES禁用
该查询将选择字符串字面值"column",其中column foo等于字符串"bar"
ANSI_QUOTES启用
该查询将选择column foo等于column bar的列列
什么时候用什么
我建议您避免使用“”,这样您的代码就独立于SQL模式 总是引用标识符,因为这是一个很好的实践(很多关于SO的问题都讨论了这个)