简单地说,如果我有一个包含两列的表(例如人和身高),则可以按人员使用“表”组中的select max(height),它将对任何人组产生最大身高,而忽略任何null_
但是,如果我的表中有更多列(height_1,height_2,height_3等),而我想简单地重现该表,并用新列显示最大高度(height_1,height_2,height_3,max_height),我很想提出。
select
height_1,
height_2,
height_3,
max(height_1, height_2, height_3) as max_height
from 'table'
但是使用这种方式,max的行为似乎有所不同,如果 any 的参数为null,则返回null。
是否存在某种使用形式,即使汇总一系列列中的单个值,它也能像汇总列时一样操作并忽略空值?
答案 0 :(得分:2)
您可以使用类似的内容:
SELECT height_1
, height_2
, height_3
, max(ifnull(height_1, 0), ifnull(height_2, 0), ifnull(height_3, 0)) AS max_height
FROM "table"