在MySQL中何时使用单引号、双引号和反勾号

在MySQL中何时使用单引号、双引号和反勾号,mysql,sql,quotes,Mysql,Sql,Quotes,我正在努力学习编写查询的最佳方法。我也理解保持一致的重要性。到目前为止,我还没有真正的思考就随机使用了单引号、双引号和反勾号 例如: $query = 'INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2)'; 此外,在上面的例子中,考虑表, COL1, Val1 < /代码>等可以是变量。 这个标准是什么?你是做什么的 我在这里已经阅读了大约20分钟关于类似问题的答案,但似乎这个问题没有明确的答案。反勾用于表和列标识符,但只

我正在努力学习编写查询的最佳方法。我也理解保持一致的重要性。到目前为止,我还没有真正的思考就随机使用了单引号、双引号和反勾号

例如:

$query = 'INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2)';

此外,在上面的例子中,考虑<代码>表,<代码> COL1,<代码> Val1 < /代码>等可以是变量。

这个标准是什么?你是做什么的


我在这里已经阅读了大约20分钟关于类似问题的答案,但似乎这个问题没有明确的答案。

反勾用于表和列标识符,但只有当标识符为,或者,当标识符包含空格字符或超出有限集的字符(见下文)时,通常建议尽可能避免使用保留关键字作为列或表标识符,以避免引用问题

$query = "INSERT INTO `table` (`id`, `col1`, `col2`) VALUES (NULL, '$val1', '$val2')";

单引号应用于字符串值,如
values()
列表中的字符串值。MySQL对字符串值也支持双引号,但其他RDBMS更广泛地接受单引号,因此使用单引号而不是双引号是一个好习惯

MySQL还希望
DATE
DATETIME
文本值以字符串形式引用,如
'2001-01-01 00:00:00'
。有关更多详细信息,请参阅文档,特别是在日期字符串中使用连字符
-
作为段分隔符的替代方法

因此,使用您的示例,我将双引号引述PHP字符串,并对值
'val1',val2'
使用单引号
NULL
是一个MySQL关键字,是一个特殊(非)值,因此不带引号

这些表或列标识符都不是保留字,也没有使用需要引用的字符,但我还是用反勾号引用了它们(稍后将详细介绍…)

RDBMS固有的函数(例如,MySQL中的
NOW()
)不应该被引用,尽管它们的参数遵循前面提到的相同字符串或标识符引用规则

Backtick (`) table & column ───────┬─────┬──┬──┬──┬────┬──┬────┬──┬────┬──┬───────┐ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ $query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`, `updated`) VALUES (NULL, 'val1', 'val2', '2001-01-01', NOW())"; ↑↑↑↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑↑↑↑↑ Unquoted keyword ─────┴┴┴┘ │ │ │ │ │ │ │││││ Single-quoted (') strings ───────────┴────┴──┴────┘ │ │ │││││ Single-quoted (') DATE ───────────────────────────┴──────────┘ │││││ Unquoted function ─────────────────────────────────────────┴┴┴┴┘ 标识符中需要回勾引用的字符: ,则不需要使用以下字符集引用(反勾选)标识符:

ASCII:
[0-9,a-z,a-z$”
(基本拉丁字母,数字0-9,美元,下划线)

您可以使用该集合之外的字符作为表或列标识符,例如包括空格,但必须引用(反勾选)它们


此外,尽管数字是标识符的有效字符,但标识符不能仅由数字组成。如果是这样,则必须用反勾号包装。

反勾号通常用于指示
标识符,并确保安全

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
例如:

Use `database`;
这里的倒勾将帮助服务器理解
数据库
实际上是数据库的名称,而不是数据库标识符

表名和字段名也可以这样做。如果用反勾号包装数据库标识符,这是一个非常好的习惯

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
检查答案以了解有关回跳的更多信息

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

现在谈谈双引号和单引号(Michael已经提到了这一点)

但是,要定义值,必须使用单引号或双引号。让我们看另一个例子

INSERT INTO `tablename` (`id, `title`) VALUES ( NULL, title1);
在这里,我故意忘记了用引号将标题1
括起来。现在,服务器将把
标题1
作为列名(即标识符)。因此,要表示它是一个值,必须使用双引号或单引号

INSERT INTO `tablename` (`id, `title`) VALUES ( NULL, 'title1');

现在,与PHP结合使用,双引号和单引号使您的查询编写时间变得更加轻松。让我们在您的问题中查看查询的修改版本

$query = "INSERT INTO `table` (`id`, `col1`, `col2`) VALUES (NULL, '$val1', '$val2')";
现在,在PHP中使用双引号,您将使变量
$val1
$val2
使用它们的值,从而创建一个完全有效的查询。像

$val1 = "my value 1";
$val2 = "my value 2";
$query = "INSERT INTO `table` (`id`, `col1`, `col2`) VALUES (NULL, '$val1', '$val2')";
将使

INSERT INTO `table` (`id`, `col1`, `col2`) VALUES (NULL, 'my value 1', 'my value 2')

如果表COL和值是变量,则有两种方法:

使用双引号
完成查询:

$query = "INSERT INTO $table_name (id, $col1, $col2)
                 VALUES (NULL, '$val1', '$val2')";

使用单引号
''

当列/值名称类似于MySQL保留关键字时,使用反勾号
`

注意:如果用表名表示列名,则使用如下反勾号:

`表格名称`
`column\u name`
(关于问题的SQL性质,上面有很好的答案,但如果您是PHP新手,这也可能是相关的。)

也许有一点很重要,PHP处理单引号字符串和双引号字符串的方式不同

单引号字符串是“文本”,几乎是WYSIWYG字符串。双引号字符串由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

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\u real\u escape\u string
的用途。(尽管准备好的语句更好。)
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
DB [XXX]> create temporary table 123451a6 (`id` char (8));
Query OK, 0 rows affected (0.03 sec)
let query = "select id, name from accounts";
//Or
let query = 'select id, name from accounts';
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 + '\'';