Mysql聚合函数count(1) sum(1)结果返回0和NULL
2024-09-01 14:37:51
1.count(1)
返回为0
如果所查询的表或者where条件筛选后得到的结果集为空,则 count(1)返回为 0
如:
select count(id) from test;
select count(id) from test where id < 0;返回为NULL
如果所查询的表或者where条件筛选后得到的结果集为空且当前层查询中使用了group by ,则 count(1)返回为 NULL
如:
select count(id) from test group by id;
select count(id) from test where id < 0 group by id;
2.sum(1)
- 返回为NULL
如果所查询的表或者where条件筛选后得到的结果集为空 ,则 sum(1)返回为 NULL
如:
select sum(id) from test;
select sum(id) from test where id < 0;
注:如果想NULL转为0返回可以使用IFNULL(expression_1,expression_2);表示如果expression_1不为NULL,则IFNULL函数返回expression_1; 否则返回expression_2的结果。
如IFNULL(sum(id),0)
最新文章
- HTML ------ 关于表单 Form
- ArcEngine 栅格数据
- Form提交是会刷新页面的
- POJ1285 Combinations, Once Again(背包 排列组合)
- react native 底部按钮切换
- 【转】JSONP简介
- CodeSmith使用总结--创建一个基础模板
- SQL Server 移动master 数据库
- poj 1679 The Unique MST 【次小生成树】【模板】
- call 和 ret 指令
- js备战春招の四のdevtool中各种错误、调试的使用技巧
- [SCOI2015]国旗计划
- 迷宫问题 (bfs广度优先搜索记录路径)
- (1)Java数据结构--图文并茂-分析优缺点
- python 读写、创建 文件的方法(必看)
- C++模拟键盘消息
- busybox microcom Segmentation fault
- [TJOI2018]智力竞赛
- Spring框架第三篇之基于XML的DI注入
- Sqlalchemy 设置表编码及引擎