MySQL randomly selected data

Source: Internet
Author: User

MYSQ Random Query data

Previously discussed this issue in the group, the comparison is interesting. MySQL's syntax is really fun. They all want to use PHP to implement the 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.

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 MySQL order by Rand () Efficiency: http://www.phpq.net/mysql/mysql-order-by-rand.html

Actual effect

mysql> select RAND();
-> 0.5925
mysql> select RAND(20);
-> 0.1811
mysql> select RAND(20);
-> 0.1811
mysql> select RAND();
-> 0.2079
mysql> select RAND();
-> 0.7888

MYSQ Random Update data

How to write a statement to update hundreds of MySQL 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: 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

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

It's OK.

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.