我正在学习编写查询的最佳方法。我也明白始终如一的重要性。到目前为止,我没有经过任何真正的思考就随意地使用单引号、双引号和反引号。
例子:
$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中有两种类型的引号:
'用于封装字符串字面量 ’用于包含表名和列名等标识符
然后是“这是一个特例”。根据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的问题都讨论了这个)
其他回答
单引号应该用于像values()列表中的字符串值。
反勾号通常用来表示标识符,也可以避免意外使用保留的关键字。
在PHP和MySQL的组合中,双引号和单引号使您的查询编写时间更容易。
(上面有关于你的问题的SQL性质的很好的答案,但如果你是PHP新手,这也可能是相关的。)
也许值得一提的是,PHP处理单引号和双引号字符串的方式是不同的……
单引号字符串是“字面量”,基本上是所见即所得字符串。双引号字符串被PHP解释为可能的变量替换(PHP中的反撇号不是确切的字符串;它们在shell中执行命令并返回结果)。
例子:
$foo = "bar";
echo 'there is a $foo'; // There is a $foo
echo "there is a $foo"; // There is a bar
echo `ls -l`; // ... a directory list
有时候不用引号是有用的……因为这会突出生成查询的代码中的问题…例如:
x和y的位置应该总是整数…
从x= y=0的表中选择*
是SQL语法错误…有点懒,但很有用……
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'。