How set 0 with MAX function when it is NULL?(为NULL时如何用MAX函数设置0?)
问题描述
我想了解如何使用 MAX 函数在属性为 NULL 时设置属性的 0 值.例如:
I would like to understand how to set 0 value of the attribute when it is NULL with MAX function. For example:
Name columns:
number - date
Values:
10 - 2012-04-04
11 - 2012-04-04
12 - 2012-04-04
13 - 2012-04-15
14 - 2012-06-21
1 - 2013-07-04
Number 是增量字段,但它在新年到来时将自己设置为 1.但结果:
Number is incremental field, but it has set itself 1 when new year has come. But result of:
SELECT (MAX(number)+1) number WHERE date LIKE "2014%"
是 NULL 而不是 1 因为 MAX(number) 是 NULL 而不是 0
is NULL and not 1 because MAX(number) is NULL and not 0
推荐答案
好吧,因为没有像 2014 年这样的日期,所以您会期望 null,因为没有的最大值实际上是没有任何意义的.
Well, as there is no date like 2014, you would expect null, because the maximum of nothing is actually not anyting.
但是这样做:
COALESCE(MAX(number),0)
这意味着:从下一个列表中获取第一个非空的东西,所以如果你的 max
为空,它会给你 0
Which means: get the first non-null thing from the next list, so if your max
is null, it'll give you 0
这篇关于为NULL时如何用MAX函数设置0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为NULL时如何用MAX函数设置0?


基础教程推荐
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01