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

The following is written in codeigniter manual .

$ this-> db-> select () takes an optional second parameter. If you set this to FALSE, CodeIgniter will not try to protect the names of fields or tables 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 the CI applications .

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

Q1. What are the differences between reverse and single quote?

Q2. Is it possible to use an IF statement in a query as described above?

Q3. What does it mean?

 IF(`value` = "", `default`, `value`) as `value` 
+4
source share
1 answer
  • In MySQL, backlinks quote names , and single quotes create strings . If you have a column named select , MySQL will throw a syntax error when using that name without backlinks - as in SELECT select FROM foo - because it will interpret it as a keyword that might not happen there.

  • This IF function can be used as a column specification in SELECT statements. See MySQL Link .

  • This function returns the value from the default column if value is an empty row. Otherwise, it returns the value from value . The result will be called value . See the MySQL link for more details.

+14
source

Source: https://habr.com/ru/post/913068/


All Articles