Null, max()/min() 用法
Table number
contains many numbers in column num including duplicated ones.
Can you write a SQL query to find the biggest number, which only appears once.
+---+
|num|
+---+
| 8 |
| 8 |
| 3 |
| 3 |
| 1 |
| 4 |
| 5 |
| 6 |
For the sample data above, your query should return the following result:
+---+
|num|
+---+
| 6 |
Note:
If there is no such number, just output null.
Method 1:
select ifnull(
(select num
from my_numbers
group by num
having count(*) = 1
order by num desc
limit 1), null) as num
Method 2:
The max()
will take care of the null
condition. no need for IFNULL
.
select max(null)
will result in null
SELECT
MAX(num) AS num
FROM
(SELECT
num
FROM
my_numbers
GROUP BY num
HAVING COUNT(num) = 1) AS t
;