How does the MySQL Database Retrieve random data from related tables?

Source: Internet
Author: User

This article mainly describes the actual operation scheme for MySQL database to retrieve random data from the relevant table. In order to achieve random data, we found the following statement in the manual, the task can be completed. The following describes the specific solution.

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

Rand says this in the manual ::

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.

 
 
  1. MySQL (the best combination with PHP)> select RAND ();
  2. -> 0.5925
  3. MySQL (the best combination with PHP)> select RAND (20 );
  4. -> 0.1811
  5. MySQL (the best combination with PHP)> select RAND (20 );
  6. -> 0.1811
  7. MySQL (the best combination with PHP)> select RAND ();
  8. -> 0.2079
  9. MySQL (the best combination with PHP)> select RAND ();
  10. -> 0.7888

You cannot use the column with the RAND () value in an order by clause, because order by will calculate the column multiple times. However, in MySQL (the best combination with PHP) 3.23, You can do: SELECT * FROM table_name order by rand (), which is conducive to obtaining a SELECT * FROM table1, table2 WHERE a = B AND c <d ORDER BY RAND () LIMIT 1000 random samples of the set.

Note that an RAND () in a WHERE clause will be re-evaluated every time the WHERE clause is executed.

But after a try, it takes 0.08 sec to execute a record table, which is slower. Then I consulted google and got the following code:

 
 
  1. SELECT * FROM table_name AS r1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM table_name)) AS id) AS r2 WHERE r1.id >= r2.id ORDER BY r1.id ASC LIMIT 5;  

The execution efficiency requires 0.02 sec. Unfortunately, only MySQL (the best combination with PHP). * And above support such subqueries.

The above content is an introduction to the MySQL database's random data extraction from the table. I hope you will gain some benefits.

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.