- 我有一个名为“Quiz”的 Mysql 表和“question”列, “答案”和“类别”。
- 我有数学、物理和化学类别,每个类别有 10 个问题。
如果我的查询是
mysql select * from Quiz
,我需要打印类似的东西'mysql select * from Quiz where category = maths %50, physics %20 and chemistry %30
如何在 Mysql 查询中完成它?请帮助我
最佳答案
这只能通过单独的SELECT
来完成。但您可以使用 UNION
将它们合并为一个。
(select * from Quiz where category='math' order by rand() limit 5)
union all
(select * from Quiz where category='physics' order by rand() limit 3)
union all
(select * from Quiz where category='chemistry' order by rand() limit 2)
您应该预先计算每个类别的问题数量。
另请注意,对于大表来说,通过 rand()
排序是一种不好的方式。
关于php - 如何获取每个类别中具有特定百分比的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30589678/