我需要从我的问题库中随机抽取一个问题。我有一个表,QUESTION,列:questionType。我需要根据用户的要求从表中随机抽取问题。
例如,他选择问题类型:['标识','多项选择']和每个问题类型的问题数:['10','20']。
因此,我需要从带有值标识的列questionType中随机查询10个问题,并从带有值多选的列questionType中查询另外20个随机问题。
我有这个SQL语句:
SELECT question FROM QUESTION WHERE questionType = 'Identification' ORDER BY RAND() LIMIT 10;
那么我该如何添加条件:
questionType = 'Multiple Choice' ORDER BY RAND() LIMIT 20
你能帮我么?谢谢!
最佳答案
尝试这个:
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中的演示。
关于mysql - MySQL查询不同的随机条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22606253/