简体   繁体   English

sql中`'和"有什么区别

[英]What's the difference between ` ' and " in sql

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.感谢您抽出宝贵时间。

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.编辑:正如所指出的,这仅适用于 MySQL。 Different database engines use different methods to encapsulate tables, solumns or reserved words.不同的数据库引擎使用不同的方法来封装表、列或保留字。

what happens when your column is called "date" ?当您的专栏被称为"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 http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM