我正在学习编写查询的最佳方法。我也明白始终如一的重要性。到目前为止,我没有经过任何真正的思考就随意地使用单引号、双引号和反引号。
例子:
$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分钟类似问题的答案,但这个问题似乎没有明确的答案。
当前回答
有时候不用引号是有用的……因为这会突出生成查询的代码中的问题…例如:
x和y的位置应该总是整数…
从x= y=0的表中选择*
是SQL语法错误…有点懒,但很有用……
其他回答
(上面有关于你的问题的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
单引号应该用于像values()列表中的字符串值。
反勾号通常用来表示标识符,也可以避免意外使用保留的关键字。
在PHP和MySQL的组合中,双引号和单引号使您的查询编写时间更容易。
这里有很多有用的答案,通常归结为两点。
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也是一个指数数。
在PHP和MySQL的组合中,双引号和单引号使您的查询编写时间变得更加容易。
$query = "INSERT INTO `table` (`id`, `col1`, `col2`) VALUES (NULL, '$val1', '$val2')";
现在,假设你在MySQL查询中使用一个直接的post变量,这样使用它:
$query = "INSERT INTO `table` (`id`, `name`, `email`) VALUES (' ".$_POST['id']." ', ' ".$_POST['name']." ', ' ".$_POST['email']." ')";
这是在MySQL中使用PHP变量的最佳实践。
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中使用引号