我在一个mysql查询中遇到问题
注意我已经检查了所有答案,请勿将此问题标记为重复
我有一个名为“questions”的表,其中包含que_id,cat_id等字段
我需要从表中获取50条记录,这些记录应该是cat_id = 1的随机30和cat_id = 2的随机20
答案 0 :(得分:0)
我想这应该这样做:
(SELECT * FROM questions
WHERE cat_id = 1
ORDER BY RAND()
LIMIT 30)
UNION ALL
(SELECT * FROM questions
WHERE cat_id = 2
ORDER BY RAND()
LIMIT 20)
答案 1 :(得分:0)
(SELECT * FROM `questions` WHERE cat_id=2 and que_id >=
(SELECT FLOOR( MAX(que_id) * RAND()) FROM `questions` ) ORDER BY que_id LIMIT 20) UNION ALL
(SELECT * FROM `questions` WHERE cat_id=1 and que_id >=
(SELECT FLOOR( MAX(que_id) * RAND()) FROM `questions` ) ORDER BY que_id LIMIT 30)