2013-01-20 47 views
-2

可能重複:
Mysql DISTINCT not working if I add another column的MySQL得到表中的數據沒有重複

mysql> select DISTINCT number, id from table order by rand() 

+------+------------+ 
| id | number  | 
+------+------------+ 
| 2 | 4   | 
| 1 | 3   | 
| 4 | 3   | 
| 3 | 4   | 
+------+------------+ 

我需要類似的東西

+------+------------+ 
| id | number  | 
+------+------------+ 
| 2 | 4   | 
| 4 | 3   | 
+------+------------+ 

刷新

+------+------------+ 
| id | number  | 
+------+------------+ 
| 3 | 4   | 
| 1 | 3   | 
+------+------------+ 

刷新....

+0

查找'集團BY' –

+0

你真的問同樣的問題兩次withi 45分鐘(除非你是滿意的非隨機行爲)? –

+0

您是否說每次刷新都會選擇上次調用時沒有選擇的重複ID?或者一個電話獲得具有最新ID的副本,而另一個具有最小ID。如果它重複三次呢? –

回答

0
SELECT 
    (SELECT tt.id 
     FROM tableX AS tt 
     WHERE tt.number = t.number 
     ORDER BY RAND() 
      LIMIT 1 
    ) AS id, 
    number 
FROM 
    tableX AS t 
GROUP BY 
    number ; 
+1

Wooooooooooooooooow,非常感謝你 –