Mysql 使用RAND()选择包含concat、group和limit的行
我有一个问题。我想要这样的唱片:Mysql 使用RAND()选择包含concat、group和limit的行,mysql,Mysql,我有一个问题。我想要这样的唱片: subject_id question_id 3 4,5 id question subject_id 1 a 1 2 b 1 3 c 2 4 d 3 5 e 3 6 f 3 我的桌子看起来像这样: subject_id question_id 3 4,5 id
subject_id question_id
3 4,5
id question subject_id
1 a 1
2 b 1
3 c 2
4 d 3
5 e 3
6 f 3
我的桌子看起来像这样:
subject_id question_id
3 4,5
id question subject_id
1 a 1
2 b 1
3 c 2
4 d 3
5 e 3
6 f 3
我试过使用
SELECT subject_id, group_concat(id)
FROM tbl_qus_bank
WHERE subject_id=3
ORDER by RAND() LIMIT 2
但它没有起作用 尝试子查询:
SELECT subject_id, group_concat(id)
FROM (SELECT qb.*
FROM tbl_qus_bank qb
WHERE subject_id = 3
ORDER by RAND() LIMIT 2
) qb
GROUP BY subject_id;
或者,您也可以使用子字符串\u index()
——如果主题总数不太大:
SELECT subject_id,
substring_index(group_concat(id) order by rand(), ',', 2)
FROM tbl_qus_bank
WHERE subject_id = 3;
ORDER BY
和LIMIT
应用于整个查询的结果。也就是说,在组_CONCAT()
已经生成其结果之后,将对其进行处理