从mysql表中选择随机行[duplicate]

时间:2022-09-21 15:47:44

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实现这样的事情?

3 个解决方案

#1


44  

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

Edit:

编辑:

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

关于MySQL RAND()函数的有用信息可以在这里找到。

#2


5  

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.

注意,使用大型数据集的rand()排序非常缓慢,但在您的例子中,这不是问题。

#3


3  

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存储在字段questine下的情况下,会随机选择10个questines吗

#1


44  

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

Edit:

编辑:

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

关于MySQL RAND()函数的有用信息可以在这里找到。

#2


5  

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.

注意,使用大型数据集的rand()排序非常缓慢,但在您的例子中,这不是问题。

#3


3  

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存储在字段questine下的情况下,会随机选择10个questines吗