How can I achieve random query of MySQL Data and random update of MySQL Data?

Source: Internet
Author: User

The following articles mainly describe how to use MySQL to randomly select data and describe the actual operation steps for random data query by MySQ and random data update by MySQ, the descriptions of the statements used in actual operations are as follows.

MySQL random query data

I have discussed this issue in the group before. It is quite interesting. mysql 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.

 
 
  1. SELECT * FROM table_name ORDER BY rand() LIMIT 5; 

The rand () function of MySQL is described in the Manual as follows:

 
 
  1. RAND()  
  2. 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.

About the efficiency of MySQL rand () function, you can refer to the MySQL Order By Rand () Efficiency: http://www.phpq.net/mysql/mysql-order-by-rand.html

Actual Effect

 
 
  1. mysql> select RAND();  
  2. -> 0.5925  
  3. mysql> select RAND(20);  
  4. -> 0.1811  
  5. mysql> select RAND(20);  
  6. -> 0.1811  
  7. mysql> select RAND();  
  8. -> 0.2079  
  9. mysql> select RAND();  
  10. -> 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.

Update is like writing 100 data records?

The correct answer is:

 
 
  1. 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.

 
 
  1. SELECT * FROM `table` ORDER BY RAND() LIMIT 5 

You can. The above content is an introduction to the implementation of MySQ random query data and MySQ random update data. I hope you will gain some benefits.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.