简体   繁体   English

反引号和单引号有什么区别? 我可以在上面的查询中使用IF语句吗?

[英]What are the differences between backtick and single quote? Can I use IF statement in a query as above?

In the codeigniter manual writes the following. codeigniter手册中写下以下内容。

$this->db->select() accepts an optional second parameter. $ this-> db-> select()接受可选的第二个参数。 If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. 如果将其设置为FALSE,CodeIgniter将不会尝试使用反引号来保护您的字段或表名称。 This is useful if you need a compound select statement. 如果您需要复合选择语句,这非常有用。

$this->db->select('(SELECT SUM(payments.amount) FROM payments 
WHERE payments.invoice_id=4) AS amount_paid', FALSE);
$query = $this->db->get('mytable');
...

And I have the following code from one of CI applications . 我从一个CI应用程序中获得以下代码。

$this->db->select('slug, type, 
IF(`value` = "", `default`, `value`) as `value`', FALSE);

Q1. Q1。 What are the differences between backtick ` and single quote '? 反引号和单引号有什么区别?

Q2. Q2。 Can I use IF statement in a query as above? 我可以在上面的查询中使用IF语句吗?

Q3. Q3。 What does this mean? 这是什么意思?

IF(`value` = "", `default`, `value`) as `value`
  1. In MySQL, backticks quote names , while single quotes create strings . 在MySQL中,反引号引用名称 ,而单引号则创建字符串 If you have a column called select , MySQL would throw an syntax error when using this name without backticks -- like in SELECT select FROM foo -- as it would interpret it as keyword which may not occur there. 如果你有一个名为select的列,那么在使用这个没有反引号的名字时,MySQL会抛出一个语法错误 - 就像在SELECT select FROM foo - 因为它会将它解释为可能不会出现在那里的关键字。

  2. This IF function can be used as a column specification in SELECT statements. 此IF 函数可用作SELECT语句中的列规范。 See the MySQL reference . 请参阅MySQL参考

  3. This function returns the value from the default column, if value is the empty string. 如果value为空字符串,则此函数返回default列中的value Else it returns the value from value itself. 否则它从value本身返回value The result will be called value . 结果将被称为value See the MySQL reference for details. 有关详细信息,请参阅MySQL参考

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

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