MySQL database implements the random data operation process in the table

Source: Internet
Author: User
The actual operations on random data retrieved from the table in the MySQL database were discussed in the group before. mySQL (the best combination with PHP) syntax can be said to be quite fun. they all wanted to use PHP to implement random queries, but retrieving multiple entries seemed to require more than two queries. MySQL database to retrieve random data from tables

The actual operations on random data retrieved from the table in the MySQL database were discussed in the group before. mySQL (the best combination with PHP) syntax can be said to be quite fun. they all wanted to use PHP to implement random queries, but retrieving multiple entries seemed to require more than two queries. MySQL database to retrieve random data from tables

The actual operations on random data retrieved from the table in the MySQL database were discussed in the group before. mySQL (the best combination with PHP) syntax can be said to be quite fun. they all wanted to use PHP to implement random queries, but retrieving multiple entries seemed to require more than two queries.

MySQL database implements random data retrieval in the table:

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

Rand said 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.

MySQL (the best combination with PHP)> select RAND ();

-> 0.5925

MySQL (the best combination with PHP)> select RAND (20 );

-> 0.1811

MySQL (the best combination with PHP)> select RAND (20 );

-> 0.1811

MySQL (the best combination with PHP)> select RAND ();

-> 0.2079

MySQL (the best combination with PHP)> select RAND ();

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

MySQL database implements random data removal from tables. However, I tried to execute a 0.08-thousand-record table, which requires sec.

Later I consulted google and got the following code:

 
 
  1. SELECT *
  2. FROM table_name AS r1 JOIN
  3. (SELECT ROUND(RAND() *
  4. (SELECT MAX(id)
  5. FROM table_name)) AS id)
  6. AS r2
  7. WHERE r1.id >= r2.id
  8. ORDER BY r1.id ASC
  9. 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 introduces how to retrieve random data from the MySQL database.

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.