我有以下查询:
$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_serialized的字段,我想对在is_serialized字段中具有值的所有行进行分组,但如果行为NULL,则不进行分组,是否可以使用我的查询来做到这一点?
答案 0 :(得分:3)
在group by子句中使用“ CASE”功能仅检查不为空值。
例如:
$this->db->group_by("CASE WHEN items.is_serialized IS NOT NULL THEN items.is_serialized END",FALSE);
答案 1 :(得分:0)
在下面将条件按is_serialized分组
$this->db->where('items.is_serialized IS NOT NULL');