sql中`'和“之间有什么区别

时间:2023-01-24 00:07:18

I have seen different ways on how to use it but I don't understand the why...

我已经看到了如何使用它的不同方法,但我不明白为什么......

say a simple SQL..

说一个简单的SQL ..

$q = "UPDATE table SET
col1 = '$var1',
col2 = '$var2'
 ";

So that is one way...

所以这是一种方式......

$q = "UPDATE table SET
`col1` = $var1,
`col2` = $var2
 ";

This does the same, but why the use of `?

这样做也一样,但为什么要使用`?

and then:

$q = "UPDATE table SET
'col1' = $var1,
'col2' = $var2
 ";

So what is the correct way to use it, when to use it, and why... then I have seen this:

那么使用它的正确方法是什么,何时使用它,为什么......然后我看到了这个:

$q = "UPDATE table SET
col1 = ".$var1.",
col2 = ".$var2";

Thank you for taking the time.

感谢您抽出宝贵时间。

2 个解决方案

#1


0  

The backtick ` is for reserved words or column names, while a normal single quote ' is to encapsulate strings.

反引号`用于保留字或列名,而正常的单引号'用于封装字符串。

You can read the detailed info here: https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

您可以在此处阅读详细信息:https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Edit: As pointed out, this is for MySQL only. Different database engines use different methods to encapsulate tables, solumns or reserved words.

编辑:正如所指出的,这仅适用于MySQL。不同的数据库引擎使用不同的方法来封装表,solumns或保留字。

#2


0  

what happens when your column is called "date" ? Mysql will not know if you have used a function wrong or if you meant a column called date

当您的专栏被称为“日期”时会发生什么? Mysql不知道你是否使用了错误的函数,或者你是否意味着一个名为date的列

It should be used often to make mysql aware your using a column and not let it do the guess work.

应该经常使用它来让mysql知道你使用一个列,而不是让它做猜测工作。

The second question, there is no difference. But you shouldnt build query's like that, instead look at prepared statements

第二个问题,没有区别。但你不应该像这样构建查询,而是查看预处理语句

http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

#1


0  

The backtick ` is for reserved words or column names, while a normal single quote ' is to encapsulate strings.

反引号`用于保留字或列名,而正常的单引号'用于封装字符串。

You can read the detailed info here: https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

您可以在此处阅读详细信息:https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Edit: As pointed out, this is for MySQL only. Different database engines use different methods to encapsulate tables, solumns or reserved words.

编辑:正如所指出的,这仅适用于MySQL。不同的数据库引擎使用不同的方法来封装表,solumns或保留字。

#2


0  

what happens when your column is called "date" ? Mysql will not know if you have used a function wrong or if you meant a column called date

当您的专栏被称为“日期”时会发生什么? Mysql不知道你是否使用了错误的函数,或者你是否意味着一个名为date的列

It should be used often to make mysql aware your using a column and not let it do the guess work.

应该经常使用它来让mysql知道你使用一个列,而不是让它做猜测工作。

The second question, there is no difference. But you shouldnt build query's like that, instead look at prepared statements

第二个问题,没有区别。但你不应该像这样构建查询,而是查看预处理语句

http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php