-2

可能重复:
如果我添加另一列,Mysql DISTINCT 不起作用

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          |
+------+------------+

刷新....

4

1 回答 1

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 ;
于 2013-01-20T23:46:04.143 回答