mysql - 如果字段不为空,则 Codeigniter 分组
问题描述
我有以下查询:
$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');
在项目表中,我有一个名为 is_serialized 的字段,我想将在 is_serialized 字段中具有值的所有行分组,但如果该行为 NULL,则不分组,是否可以使用我的查询来这样做?
解决方案
在 group by 子句中使用“CASE”函数仅检查非空值。
例如:
$this->db->group_by("CASE WHEN items.is_serialized IS NOT NULL THEN items.is_serialized END",FALSE);
推荐阅读
- excel - 从 Access 中搜索工作簿中的所有 Excel 工作表,并使用 VBA 事件表单中的字符串
- c++ - 如何防止构造函数中的 bool 到 int 转换?
- json - Pyspark读取json:java.net.URISyntaxException:绝对URI中的相对路径
- clojure - 如何为redis编写一个模仿`lrem`行为的转换器
- android - react-native - 以编程方式禁用自动更新应用程序
- email - G-Suite 每天限制为 100 封电子邮件
- javascript - toLocaleDateString 时间格式问题(Firefox 与 Chrome)
- java - 在 Java 中获取过去 6 个月的日期/时间范围每月明智
- python - “AssertionError:blk ref_locs 中的间隙” - Python - Pandas - 多线程 - 2 个数据帧
- c# - 如何从 httplclient 调用中获取 json?