令行比方說,我有這個表:mysql的:通過值頻率
+----+------+---------+
| Id | Item | Country |
+----+------+---------+
| 1 | b123 | Austria |
| 2 | a123 | Italy |
| 3 | b990 | Germany |
| 4 | h231 | Austria |
| 5 | y233 | France |
| 6 | u223 | Austria |
| 7 | p022 | Spain |
| 8 | d133 | Italy |
| 9 | w112 | Germany |
| 10 | j991 | Austria |
+----+------+---------+
我想做的事該表的SELECT
並責令由Country
被重複最多的結果。 所以預期輸出應該是:
+----+------+---------+
| Id | Item | Country |
+----+------+---------+
| 1 | b123 | Austria |
| 4 | h231 | Austria |
| 6 | u223 | Austria |
| 10 | j991 | Austria |
| 2 | a123 | Italy |
| 8 | d133 | Italy |
| 3 | b990 | Germany |
| 9 | w112 | Germany |
| 5 | y233 | France |
| 7 | p022 | Spain |
+----+------+---------+
我怎麼能這樣做?
我已經試過這樣:
SELECT * FROM items WHERE Item != '' GROUP BY Item HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC
但是,這將返回是這樣的:
+----+------+---------+
| Id | Item | Country |
+----+------+---------+
| 1 | b123 | Austria |
| 8 | d133 | Italy |
| 3 | b990 | Germany |
| 5 | y233 | France |
| 7 | p022 | Spain |
+----+------+---------+
您是否嘗試在'SELECT'之後添加'DISTINCT'關鍵字? – Sablefoste