mysql从表中获取数据而不重复

时间:2013-01-20 23:23:20

标签: mysql

  

可能重复:
  Mysql DISTINCT not working if I add another column

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

刷新....

1 个答案:

答案 0 :(得分: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 ;