Home  >  Article  >  Backend Development  >  PHP method to randomly obtain 20 records from millions of data in the database

PHP method to randomly obtain 20 records from millions of data in the database

墨辰丷
墨辰丷Original
2018-05-23 15:44:551952browse

This article mainly introduces the method of PHP to randomly obtain 20 records from millions of data in the database, involving simple database query and random number operation related skills in PHP. Friends who need it can refer to it

1. First count the number of records in the database (this is used as a data cache, such as re-counting once every hour),

2. Based on the total number of records, randomly take out 20 records at once (of course This is equivalent to paging. If the requirements are not high, this is the fastest. This is what I use);

There is another method, randomize 20 times and repeat 20 times.

For example:

$sum=800000;//得到总条数
//循环取数据
for($i=1;$i<=20;$i++){
  $offset=mt_rand(1,$sum);
  //控制重复对比的,请自行实现
  //数据库取数据的,请自行实现
  $sql="select * from table limit $offset,1";
  echo $sql;
}

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

Detailed explanation of the installation and use of extensions in the Yii framework of PHP_php tips

PHPHow to record the time spent on a page_php tips

PHP How to record the time spent on the page_php tips

The above is the detailed content of PHP method to randomly obtain 20 records from millions of data in the database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn