来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/biggest-single-number 【侵删】
ExampleA(只出现一次的最大数字)
表 my_numbers 的 num 字段包含很多数字,其中包括很多重复的数字。
你能写一个 SQL 查询语句,找到只出现过一次的数字中,最大的一个数字吗?
+---+
|num|
+---+
| 8 |
| 8 |
| 3 |
| 3 |
| 1 |
| 4 |
| 5 |
| 6 |
对于上面给出的样例数据,你的查询语句应该返回如下结果:
+---+
|num|
+---+
| 6 |
注意:
如果没有只出现一次的数字,输出 null 。
解题思路(分组 count ifnull)
select ifnull((select n.num from (select num,count(*) numcount from my_numbers group by num order by num Desc) n where n.numcount=1 limit 1),null) num;