2016-01-13 58 views
-1

得到的记录随机数我有一个表:SQL查询从两类

id type_id 
1 1 
2 1 
3 2 
4 1 
5 2 
6 2 
7 1 
8 1 
9 2 

等等

我需要得到1型和3个随机记录同样数量的类型2的随机记录。我怎样才能得到它与一个查询?

回答

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

只是,我一定要得到随机值 – Barba

+0

,我需要得到的东西是这样的: (SELECT * FROM your_table where type_id = 1 ORDER BY RAND()限制3) 工会全部 (select * from your_table where type_id = 2 ORDER BY RAND()limit 3) – Barba

+0

然后恰好使用:) –

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