codeigniter-group-by如果字段不为null

dtcbnfnu  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(318)

我有以下疑问:

$this->db->select('SUM(cost_price) AS cost_price,SUM(cost_price) AS total,employees.username AS staff_name');
        $this->db->from('items');
        $this->db->join('customers', 'customers.person_id = items.customer_id');
        $this->db->join('employees', 'employees.person_id = items.staff_id');
        $this->db->join('people', 'people.person_id = customers.person_id');
 $this->db->group_by('items.is_serialized');

在items表中,我有一个名为is\u serialized的字段,我想对is\u serialized字段中具有值的所有行进行分组,但不分组如果该行为null,是否可以使用我的查询进行分组?

zlwx9yxi

zlwx9yxi1#

在GROUPBY子句中使用“case”函数只检查非空值。
如:

$this->db->group_by("CASE WHEN items.is_serialized IS NOT NULL THEN items.is_serialized END",FALSE);
mfuanj7w

mfuanj7w2#

放在要分组的条件被序列化的位置下方

$this->db->where('items.is_serialized IS NOT NULL');

相关问题