Home >Backend Development >PHP Tutorial >php+mysql realizes random database rearrangement example, phpmysql rearrangement example_PHP tutorial
The example in this article implements the method of random rearrangement of the php+mysql database. All the data in the table can be randomly read out once and then randomly saved to another table, thus achieving the function of random recording.
The main implementation code is as follows:
I hope this article will be helpful to everyone’s PHP programming design.
1) Just use sql statement to randomly take out 5 records;
mysql is as follows: "select * from youtable order by rand() limit 5";
2) Also randomly pick 5-10 Bar:
$num=rand(5,10);
$sql="select * from youtable order by rand() limit $num";
ps: Why is it not a PHP classification problem?
It is not a good habit to directly use select * from student order by rand() limit 5. This must take into account the size of the database. For example, if the data volume is more than 10W, the query efficiency is very low and the resources It also consumes a lot! You can consider combining php with mysql to implement it. For example: first use mysql's count() to calculate the total number of students $sum_num, assign it to a variable in php, and then generate this total number in php. Random numbers (this involves whether your 5 pieces of data are 5 consecutive random numbers or 5 scrambled random numbers). If you generate continuous data here, you only need to generate a random number less than $sum_num-5. ! Based on this random number, you can limit out 5 items; if you need to generate scrambled random numbers here, you need to generate 5 different random numbers that are smaller than $sum_num, and finally query in the database based on these random numbers. The first method In this case, limit random number, 5; is used; in the second case, in (random number 1, random number 2, random number 3, random number 4, random number 5) is used; in the case of large amount of data, such efficiency In terms of resource consumption, it is much better than select * from student order by rand() limit 5. Of course, if the amount of data is not large, you can use this! Just to spread the knowledge to you! I wish you a happy study! (For the time being, I assume that you can use PHP to generate random numbers and the random numbers generated by PHP here must be an integer greater than 0. If not, you can use Baidu or Google first, and finally you can continue to leave me a message)!
Because your ID may be missing, some methods are not applicable! I won’t list them one by one!