题目:https://leetcode-cn.com/problems/biggest-single-number/
表 my_numbers 的 num 字段包含很多数字,其中包括很多重复的数字。
你能写一个 SQL 查询语句,找到只出现过一次的数字中,最大的一个数字吗?
+---+
|num|
+---+
| 8 |
| 8 |
| 3 |
| 3 |
| 1 |
| 4 |
| 5 |
| 6 |
对于上面给出的样例数据,你的查询语句应该返回如下结果:
+---+
|num|
+---+
| 6 |
注意:
如果没有只出现一次的数字,输出 null 。
代码:
SELECT (SELECT num FROM my_numbers GROUP BY num HAVING count(*) = 1 ORDER BY num DESC LIMIT 1) AS num
起初只写了SELECT num FROM my_numbers GROUP BY num HAVING count(*) = 1 ORDER BY num DESC LIMIT 1,但是当遇到所有数值都有多个的情况返回了空,题目要求此种情况返回null,需要在外面再加层select