mysql case when多条件同时满足的多个and组合嵌套的情况,判断空is null --- 系列一 https://blog.csdn.net/qb170217/article/details/81504578
方法一:
SELECT id, time, type,
CASE when (reason is null or reason = '') and type = '驳回' THEN '未填写驳回理由'
ELSE reason
END reason
from workFlow
order by time desc;
方法二:
SELECT id,time,type,
CASE when type = '驳回' and (reason is null or reason = '') THEN '未填写驳回理由'
ELSE reason
END reason
from workFlow
order by time desc;
方法三:
SELECT id,time,type,
CASE when type = '驳回' and reason is null or reason = '' THEN '未填写驳回理由'
ELSE reason
END reason
from workFlow
order by time desc;
方法四:
SELECT id,time,type,
CASE when (type = '驳回' and reason is null) or reason = '' THEN '未填写驳回理由'
ELSE reason
END reason
from workFlow
order by time desc;
为什么上面方法三, 四和方法二是一样结果? 请大神指点 分享思路...
我的理解:方法三 先找type='驳回'的结果,再与 reason为null,空白串'' 做 ”与“判断,所以与方法二结果一样。
在我预想的是两种结果组合:条件1:type='驳回' and reason is null, 条件2: 单独的 reason='', 而实际结果与我预想的不一样。
个人觉得, 1和2一样结果,3和4是另一个一样的结果,但与实际测试结果不一样。事实上,上面4种方法一样的结果。还得继续努力呀!
方法五:
SELECT id,time,type,
CASE when reason = '' or reason is null and type = '驳回' THEN '未填写驳回理由'
ELSE reason
END reason
from workFlow
order by time desc;
方法六:
SELECT id,time,type,
CASE when reason is null or reason = '' and type = '驳回' THEN '未填写驳回理由'
ELSE reason
END reason
from workFlow
order by time desc;
我滴神,我已经晕啦。。。。
方法1,2,3,4,5结果居然一样, 不明白方法3,4,5的结果为啥与方法1,2结果一样。。。。55555, 个人觉得方法1,2结果一样,方法3,4,5,6结果一样.........
方法6符合预想的结果,能理解!
当type=‘驳回’,就把为null 或 为空串 的reason 赋值为‘未填写驳回理由’,对已填写驳回理由,就直接返回reason; 当type为其它类型时,不需要填写理由,所以返回结果保留reason的值,即为空。
方法一二三四五的结果如下:
方法六的结果如下: