我正在学习编写查询的最佳方法。我也明白始终如一的重要性。到目前为止,我没有经过任何真正的思考就随意地使用单引号、双引号和反引号。
例子:
$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。
免责声明:因此,对于那些完全不熟悉数据库的人以及可能不理解已经描述的技术术语的人,我认为我将添加这一点作为“供参考”类型的回答。
其他回答
在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'。
MySQL和PHP中的字符串文字是相同的。
字符串是包含在字节或字符中的序列 单引号(" ' ")或双引号(" " ")字符。
如果你的字符串包含单引号,那么你可以使用双引号来引用字符串,或者如果它包含双引号,那么你可以使用单引号来引用字符串。但如果字符串同时包含单引号和双引号,则需要转义用于字符串引号的字符串。
大多数情况下,我们对SQL字符串值使用单引号,因此我们需要对PHP字符串使用双引号。
$query = "INSERT INTO table (id, col1, col2) VALUES (NULL, 'val1', 'val2')";
你可以使用PHP双引号字符串中的变量:
$query = "INSERT INTO table (id, col1, col2) VALUES (NULL, '$val1', '$val2')";
但是如果$val1或$val2包含单引号,则会使SQL出错。所以你需要在sql中使用它之前转义它;这就是mysql_real_escape_string的作用。(尽管事先准备好的陈述更好。)
这里有很多有用的答案,通常归结为两点。
BACKTICKS(')用于标识符名称周围。 单引号(')用于值周围。
正如@MichaelBerkowski所说
反勾号用于表和列标识符,但是 只有当标识符是MySQL保留关键字时才需要,或者 当标识符包含空白字符或字符时 超出限定的范围(见下文)通常建议避免使用 尽可能使用保留的关键字作为列或表标识符, 避免引用问题。
但也有一种情况,标识符既不能是保留关键字,也不能包含空格或超出限定集的字符,但必须在它们周围加上反引号。
例子
123E10是一个有效的标识符名称,也是一个有效的INTEGER字面值。
假设我想创建一个名为123456e6的临时表。
反刻度没有错误。
DB [XXX]> create temporary table `123456e6` (`id` char (8));
Query OK, 0 rows affected (0.03 sec)
不使用反勾号时出错。
DB [XXX]> create temporary table 123451e6 (`id` char (8));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '123451e6 (`id` char (8))' at line 1
但是,123451a6是一个非常好的标识符名称(没有反勾号)。
DB [XXX]> create temporary table 123451a6 (`id` char (8));
Query OK, 0 rows affected (0.03 sec)
这完全是因为1234156e6也是一个指数数。
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中使用引号
(上面有关于你的问题的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