【问题标题】:IF statement on where(codeigniter active record)关于 where(codeigniter active record) 的 IF 语句
【发布时间】:2016-02-25 05:15:51
【问题描述】:

我想问一下活动记录代码点火器 是否可以在 where active record 中使用 IF 语句?

$this->db->where("IF((month(a.create_date)) < month(now()) , d.approve_by, '') like $username" );

所以,我想获取数据approval_by,但create_date必须是当前月份的前一个月

我用mysql试过了,可以的。

IF(month(a.create_date) < month(NOW()),b.approve_by,'') = 'someusername'

当我尝试在活动记录上实施时,没有显示任何内容。

感谢您的帮助:)

【问题讨论】:

标签: php mysql codeigniter activerecord continuous-integration


【解决方案1】:

来自Codeigniter Userguide

$this-&gt;db-&gt;where() 接受可选的第三个参数。如果您将其设置为 FALSE,CodeIgniter 将不会尝试使用反引号保护您的字段或表名。

$this-&gt;db-&gt;where("IF((month(a.create_date)) &lt; month(now()) , d.approve_by, '') like $username", NULL, FALSE);

【讨论】:

    【解决方案2】:

    我们可以使用下面提到的MYSQLCASE

    
      $this->db->select(
         '(CASE
                WHEN orderdetails.ProductID = 0 THEN dealmaster.deal_name
                WHEN orderdetails.DealID = 0 THEN products.name
            END) as product_name'
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-26
      • 2015-09-17
      • 2013-04-11
      • 2014-12-24
      • 2017-11-30
      • 1970-01-01
      • 2015-07-31
      • 2013-02-27
      相关资源
      最近更新 更多