反引号和单引号有什么区别?我可以在上面的查询中使用 IF 语句吗? [英] What are the differences between backtick and single quote? Can I use IF statement in a query as above?

查看:36
本文介绍了反引号和单引号有什么区别?我可以在上面的查询中使用 IF 语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

codeigniter 手册中写了以下内容.

$this->db->select() 接受一个可选的第二个参数.如果你设置它为 FALSE,CodeIgniter 不会尝试保护您的字段或表名带反引号.这很有用,如果你需要一个复合选择语句.

$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');
...

我有以下代码来自 其中一个 CI 应用程序.

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

第一季度.反引号 ` 和单引号 ' 有什么区别?

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

第 2 季度.我可以在上面的查询中使用 IF 语句吗?

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

第三季度.这是什么意思?

Q3. What does this mean?

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

推荐答案

  1. 在 MySQL 中,反引号引用 名称,而单引号创建字符串.如果您有一个名为 select 的列,MySQL 会在使用此名称而不使用反引号时抛出语法错误——就像在 SELECT select FROM foo 中一样——因为它会将它解释为可能不会出现在那里的关键字.

  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.

此 IF 函数 可用作 SELECT 语句中的列规范.请参阅MySQL 参考.

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

如果value 是空字符串,则此函数返回default 列中的值.否则它返回 value 本身的值.结果将被称为value.有关详细信息,请参阅 MySQL 参考.p>

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.

这篇关于反引号和单引号有什么区别?我可以在上面的查询中使用 IF 语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆