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

只愿长相守 提交于 2019-12-17 06:51:06

问题


In the codeigniter manual writes the following.

$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. 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.

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

Q1. What are the differences between backtick ` and single quote '?

Q2. Can I use IF statement in a query as above?

Q3. What does this mean?

IF(`value` = "", `default`, `value`) as `value`

回答1:


  1. In MySQL, backticks quote names, while single quotes create strings. 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.

  2. This IF function can be used as a column specification in SELECT statements. See the MySQL reference.

  3. This function returns the value from the default column, if value is the empty string. Else it returns the value from value itself. The result will be called value. See the MySQL reference for details.



来源:https://stackoverflow.com/questions/2122721/what-are-the-differences-between-backtick-and-single-quote-can-i-use-if-stateme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!