php - 如何获取每个类别中具有特定百分比的数据

标签 php mysql sql function

  1. 我有一个名为“Quiz”的 Mysql 表和“question”列, “答案”和“类别”。
  2. 我有数学、物理和化学类别,每个类别有 10 个问题。
  3. 如果我的查询是 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/

相关文章:

PHP echo 总是转到 JQuery $.ajax 错误 block

php - 通过确保创建 session 的唯一来源应该是安全的随机生成器来防止 session 固定

php - 计算 id,插入,然后在同一个表 mysql 的另一列中按数字范围更新

sql - Oracle 从 SYSDATE 中减去 5 个工作日

mysql - 使用codeigniter的事件记录语法增加mysql数据库的字段

php - 使用第一个表的输出从第二个表中选择特定数据

php - 在 View 中调用 View 无法正常工作

mysql - 选择 * 时增加的时间是什么原因?

mysql - 与属性和代理键的多对多关系

sql - SQL Server 中每行超过 8060 字节会对性能产生什么影响?