鍍金池/ 問答/數(shù)據(jù)庫/ mysql 如何查詢唯一且最小值?

mysql 如何查詢唯一且最小值?

比如表a如下結構
id score
1 1
2 1
3 2
4 4

我要查詢score首先唯一(不重復),(然后)且為最小,返回的正確結果id應該就是3

那么sql語句該怎么寫呢?請教大神。

回答
編輯回答
扯機薄

SELECT id, score from table GROUP BY score HAVING COUNT(score) = 1 ORDER BY score LIMIT 1

2017年5月16日 13:56
編輯回答
伴謊

SELECT id FROM table where score = (select score from table GROUP BY score HAVING COUNT(score) = 1 ORDER BY score LIMIT 1)

2018年9月14日 23:51