MYSQL如何随机读取多个分类中的某一条数据

发布于 2013-05-31 来源 原文链接

更新于 2015-10-13

mysql> Select id, name ,val,memo from (select * from tb order by rand())t group by name limit 0,30 ;
+—-+——+——+——+
| id | name | val | memo |
+—-+——+——+——+
| 1 | a | 2 | a2 |
| 6 | b | 2 | b2 |
| 7 | c | 1 | c1 |
+—-+——+——+——+
3 rows in set (0.00 sec)

mysql> Select id, name ,val,memo from (select * from tb order by rand())t group by name limit 0,30 ;
+—-+——+——+——+
| id | name | val | memo |
+—-+——+——+——+
| 2 | a | 1 | a1 |
| 4 | b | 1 | b1 |
| 7 | c | 1 | c1 |
+—-+——+——+——+
3 rows in set (0.01 sec)

mysql> Select id, name ,val,memo from (select * from tb order by rand())t group by name limit 0,30 ;

+—-+——+——+——+
| id | name | val | memo |
+—-+——+——+——+
| 3 | a | 3 | a3 |
| 6 | b | 2 | b2 |
| 7 | c | 1 | c1 |
+—-+——+——+——+