问题
表格number在字段num中包含许多的数字(包含重复)。
写一条SQL语句求出只出现过一次的最大的数。
+---+
|num|
+---+
| 8 |
| 8 |
| 3 |
| 3 |
| 1 |
| 4 |
| 5 |
| 6 |
对于上面的例子,结果应返回:
+---+
|num|
+---+
| 6 |
注意:
如果不存在这样的数,直接返回null。
审题
按num分组统计每个num出现的个数
SELECT num, COUNT(num)
FROM number
GROUP BY num;
选出出现次数为1的num
SELECT num
FROM number
GROUP BY num
HAVING COUNT(num) = 1;
再取最大值即可
SELECT MAX(num) AS `num`
FROM (SELECT num
FROM number
GROUP BY num
HAVING COUNT(num) = 1) tmp;