php 从mysql表中选择随机行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8779585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:29:15  来源:igfitidea点击:

Select random rows from mysql table

phpmysqlsql

提问by Mateus

Possible Duplicate:
Selecting Random Rows in MySQL

可能的重复:
在 MySQL 中选择随机行

I'm creating a simple web application using PHP and MySQL. In it, I need to randomly select a small set of rows from a table in a random order. How can I achieve such thing using MySQL?

我正在使用 PHP 和 MySQL 创建一个简单的 Web 应用程序。在其中,我需要以随机顺序从表中随机选择一小组行。我怎样才能使用 MySQL 实现这样的事情?

回答by Martin Gallagher

SELECT * FROM table ORDER BY RAND() LIMIT 10;

Edit:

编辑:

Useful information about the MySQL RAND() function can be found here.

可以在此处找到有关 MySQL RAND() 函数的有用信息。

回答by Nicola Cossu

select * from table order by rand() limit 10

Note that order by rand()with large dataset is very slow but in your case it's not a problem.

请注意,order by rand()大型数据集非常慢,但在您的情况下,这不是问题。

回答by Vijeenrosh P.W

you could do that using RAND() function .

你可以使用 RAND() 函数来做到这一点。

SELECT questine FROM tablename ORDER BY RAND() LIMIT 10

will select 10 questines at random under assumption the questine is stored under field questine

假设 questine 存储在 field questine 下,将随机选择 10 个 questine