IF 语句在哪里(codeigniter 活动记录)

IF statement on where(codeigniter active record)

我想问一下 active record codeigniter 是否可以在where active record中使用IF语句?

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

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

我试过mysql,确实可以。

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

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

感谢您的帮助:)

来自 Codeigniter Userguide

$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.

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

我们可以使用 MYSQLCASE,如下所述。


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