为什么要转化?
当空集具有二义性时,如没有找到 和 找到了但是数值为0 。 如何解决 —— 允许返回值:[1],[2],[3],[0],[null] 。 这组记录。0 代表找到了但是数值为0 ,null 代表没有找到 此时空集代表 null ,不再具有二义性。
LeetCode题目
CASE WHEN 对结果集处理
select
(case
when count(a.num) = 0 then null
else a.num
end) as num
from
(select num from MyNumbers group by num having count(num) = 1 order by num desc limit 1) a;
聚合函数对结果集处理
count(num) 的返回值为0,其余为null
SELECT MAX(num) AS 'num'
FROM
(
SELECT num
FROM mynumbers
GROUP BY num
HAVING COUNT(num) = 1
) AS tmp
ifnull()对结果集处理
SELECT
IFNULL(
(SELECT
num
FROM
my_numbers
GROUP BY num
HAVING COUNT(*) = 1
ORDER BY num DESC
LIMIT 1),
NULL
) AS num ;
|