This article mainly describes how to implement the actual operation process of MySQL random query data and MySQL random update data, we all know that MySQL's random query data and MySQL's random update data are often used in actual operations, so we have a good understanding of the actual operations.
MySQL random query data
I have discussed this issue in the group before. It is quite interesting. mySQLl syntax is really fun. they all wanted to use PHP to implement random queries, but retrieving multiple entries seemed to require more than two queries. after reading the manual, find the following statement to complete the task.
- SELECT * FROM table_name ORDER BY rand() LIMIT 5;
The rand () function of MySQL is described in the Manual as follows:
RAND ()
RAND (N)
Returns a random floating point value ranging from 0 to 1.0. If an integer parameter N is specified, it is used as a seed value.
For the efficiency of the rand () function of MySQLL, you can refer to "MySQLL Order By Rand () Efficiency": http://www.phpq.net/MySQLl/MySQLl-order-by-rand.html
Actual Effect
- MySQLl> select RAND();
- -> 0.5925
- MySQLl> select RAND(20);
- -> 0.1811
- MySQLl> select RAND(20);
- -> 0.1811
- MySQLl> select RAND();
- -> 0.2079
- MySQLl> select RAND();
- -> 0.7888
MySQL random update data
How can I write a statement to update hundreds of MySQL Data Records!
To test the MySQL database, there is a database with tens of thousands of data records. How to Write a PHP file and update several hundred pieces of information each time? I write a loop to update one piece of information at a time, in this way, I can use WHILE to write data. How can I write an update like 100 data records?
The correct answer is:
- UPDATE cdb_posts SET views = rand();
In the insert command, rand () is used in value (). Note whether the field width is sufficient to always think that MySQL randomly queries several pieces of data.
- SELECT * FROM `table` ORDER BY RAND() LIMIT 5
You can
Original article title: MySQL randomly selected data
Connection: http://www.cnblogs.com/jinrize/archive/2009/11/23/1608986.html