Mysql查询不同的随机条件

时间:2014-03-24 10:07:30

标签: mysql sql node.js random

我需要从我的问题库中随机提问。我有一个表,QUESTION,列:questionType。我需要根据用户的要求从我的表中随机提问。

例如,他选择,questionType:[' Identification',' Multiple Choice']和每个问题的问题数量类型:[' 10',&# 39; 20'。]

所以我需要从带有值识别的问题类型列中随机查询10个问题,并从值类型为多选的列类型问题中查询另外20个随机问题。

我有这个sql语句:

SELECT question FROM QUESTION WHERE questionType = 'Identification' ORDER BY RAND() LIMIT 10;

那么如何添加条件:

questionType = 'Multiple Choice' ORDER BY RAND() LIMIT 20
你能帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:3)

试试这个:

SELECT question FROM 
(
(SELECT question FROM QUESTION WHERE questionType = 'Identification' ORDER BY RAND() LIMIT 10)
UNION ALL
(SELECT question FROM QUESTION WHERE questionType = 'Multiple Choice' ORDER BY RAND() LIMIT 20)
) T
ORDER BY RAND()

请参阅SQL Fiddle中的演示。