MySQL random query data and random update data implementation code _MYSQL

Source: Internet
Author: User
Tags rand
MySQL Random query data
Previously discussed this problem in the group, the comparison is interesting. MYSQLL's grammar is really fun. They all want to use PHP to achieve random, but take out more than two times as if to do more than a query. Turn the manual, find the following statement, you can complete the task.
Copy Code code as follows:

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

The rand () function of MySQL says so in the manual:
RAND () rand (N)
Returns a random floating-point value in the range 0 through 1.0. If an integer parameter n is specified, it is used as a seed value.
For the efficiency of MySQL's rand () function, you can refer to the MYSQLL Order by Rand () Efficiency: http://www.phpq.net/MySQLl/MySQLl-order-by-rand.html
Actual effect
Copy Code code as follows:

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 to write a statement can update hundreds of MYSQLL data!
Need to test the MySQL database, which has a database of tens of thousands of data, how to write a PHP file every time update hundreds of information, I am writing a cycle to update a message, so I know with a while to write on it, if an update like 100 data changes how to write it?
The correct answer is:
Copy Code code as follows:

UPDATE cdb_posts SET views = rand ();

The gas is dead. In the Insert command, the value () is in rand (), note that the field width is enough to always think MySQL randomly query a few data, use
Copy Code code as follows:

SELECT * from ' table ' ORDER by RAND () LIMIT 5

On it, the above related content is to MYSQLL random query data, MySQL random update data. The introduction, hope you can have some gains.
Related Article

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.