表数据库中的动态随机行

时间:2016-03-25 15:15:46

标签: php mysql random

考虑我有3个问题类型;真或假ToF,多选MC并完成。

现在我想随机获得10个问题的真或假,5个问题的多项选择和2个问题的完成(这些数字不是静态的,它将是执行函数的变量)。

我的表是这样的:

+-----+----------------------------------------------------+----------+--------+
| id  | question                                           | type     | examId |
+-----+----------------------------------------------------+----------+--------+
| 1   | true or false question 1                           | ToF      |     1  |
| 2   | true or false question 2                           | ToF      |     1  |
| 3   | true or false question 3                           | ToF      |     1  |
| 4   | true or false question 4                           | ToF      |     1  |
| 5   | true or false question 5                           | ToF      |     1  |
| 6   | true or false question 6                           | ToF      |     1  |
| 7   | true or false question 7                           | ToF      |     1  |
| 8   | true or false question 8                           | ToF      |     1  |
| 9   | true or false question 9                           | ToF      |     1  |
| 10  | true or false question 10                          | ToF      |     1  |
| 11  | true or false question 11                          | ToF      |     1  |
| 12  | true or false question 12                          | ToF      |     1  |
| 13  | multi choice question 1                            | MC       |     1  |
| 14  | multi choice question 2                            | MC       |     1  |
| 15  | multi choice question 3                            | MC       |     1  |
| 16  | multi choice question 4                            | MC       |     1  |
| 17  | multi choice question 5                            | MC       |     1  |
| 18  | multi choice question 6                            | MC       |     1  |
| 19  | multi choice question 7                            | MC       |     1  |
| 20  | multi choice question 8                            | MC       |     1  |
| 21  | complete question 1                                | complete |     1  |
| 22  | complete question 2                                | complete |     1  |
| 23  | complete question 3                                | complete |     1  |
| 24  | complete question 4                                | complete |     1  |
| 25  | complete question 5                                | complete |     1  |
| 26  | complete question 6                                | complete |     1  |
| 27  | complete question 7                                | complete |     1  |
+-----+----------------------------------------------------+----------+--------+

到目前为止我尝试的是为每种类型编写查询

SELECT * FROM question WHERE type = 'ToF' ORDER BY rand() LIMIT 10
SELECT * FROM question WHERE type = 'MC' ORDER BY rand() LIMIT 5
SELECT * FROM question WHERE type = 'complete' ORDER BY rand() LIMIT 2

我只能使用一个查询吗?

将所有结果按类型排序然后通过PHP得到我想要的随机结果是不是很好?

1 个答案:

答案 0 :(得分:2)

您可以在一个查询中执行此操作。 。 。甚至最后的随机化:

(SELECT * FROM question WHERE type = 'ToF' ORDER BY rand() LIMIT 10
) UNION ALL
(SELECT * FROM question WHERE type = 'MC' ORDER BY rand() LIMIT 5
) UNION ALL
(SELECT * FROM question WHERE type = 'complete' ORDER BY rand() LIMIT 2
)
ORDER BY rand();

这是一种合理的方法 - 如果您只有少数预先知道的类型。