我有一张桌子:
编号
id type_id
1 1
2 1
3 2
4 1
5 2
6 2
7 1
8 1
9 2
等...
我需要获得3个类型1的随机记录和相同数量的类型2的随机记录。如何通过一个查询获取它?
答案 0 :(得分:1)
(select * from your_table where type_id = 1 order by rand() limit 3)
union all
(select * from your_table where type_id = 2 order by rand() limit 3)
答案 1 :(得分:0)
使用MySQL
SELECT <<columns>> FROM <<table_name>>
ORDER BY RAND()
LIMIT <<count>>
使用Oracle
SELECT * FROM (SELECT * FROM <<table_name>> ORDER BY
SYS.DBMS_RANDOM.VALUE) WHERE ROWNUM <<Count>>