在MySQL中选择随机行 [英] Selecting Random Rows in MySQL

查看:58
本文介绍了在MySQL中选择随机行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个测验网站,并且我有一个存储所有问题的数据库. 测验有不同类型,例如数学,科学,历史等.所有问题都存储在一个表中.

I am developing a quiz website, and I have a database which stores all of the questions. There are different types of quizzes, like math, science, history, etc. All of the questions are stored in one table.

我的问题表如下:

questions ( qno(int)  ,type(int), question, .... ,... )

qno是主键,type用于跟踪测验类型.

qno is the primary key, and type is used to keep track of the quiz type.:

if type = 1 (math)
 type = 2(science)

现在,我想为每种测试选择一些随机问题. 例如,我可能只想为数学测试选择一些随机的20个问题.

Now, I want to select some random questions for each type of test. For example, I may want to select some random 20 questions for only the math test.

MySQL可以选择随机行吗?

Does MySQL have some way to select random rows?

推荐答案

您可以使用

You can use the rand function in MySQL to order the rows, and then take the top 10 (or however many you want) with limit.

select * from table order by rand() limit 10

如果您只想数学问题:

select * from table where type = 1 order by rand() limit 10

这篇关于在MySQL中选择随机行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆