简体   繁体   English

PHP + MySQL-随机列出MySQL表中的所有数据

[英]PHP + MySQL - Randomly list all the data from MySQL table

I got a table named clients in MySql. 我在MySql中得到了一个名为客户的表。 I want to list all the data from that table randomly in a php webpage. 我想在php网页中随机列出该表中的所有数据。 Is it possible to random the data whenever user visit the php webpage? 每当用户访问php网页时,是否可以将数据随机化?

Thank you :D 谢谢:D

You could do it like this: 您可以这样做:

SELECT * FROM mytable ORDER BY RAND();

This is mentioned in an example here . 这是在此处的示例中提到的。 As mentioned in the documentation , rand() doesn't generate perfectly random numbers/sequence (but it will probably suffice for practical use on a website). 文档中所述, rand()不会生成完全随机的数字/序列(但可能足以在网站上实际使用)。

You could also use limit to select only N (eg 5) records: 您还可以使用limit仅选择N个(例如5个)记录:

SELECT * FROM mytable ORDER BY RAND() LIMIT 5;

Add an ORDER BY RAND() clause to your query. 在查询中添加一个ORDER BY RAND()子句。

One warning though, this sorting will not be very efficient... 一个警告是,这种分类将不会非常有效。

SELECT * FROM clients
ORDER BY RAND()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM