Randomly retrieve a record from the mysql DATA table

Source: Internet
Author: User

Of course, the last part of my code is wrong. It is not a real random data fetch. Back home, hurry up to Baidu, so that we know how to randomly retrieve data ..

The rest of the code is not mentioned. Here we will show you the core data table search code:

The Code is as follows:  

Select * from table name order by rand () limit 1; // here, 1 is the number of retrieved data records

But how can we improve the efficiency of data retrieval on the Internet?

Search for Google. Basically, data is randomly obtained by querying max (id) * rand () on the Internet.

The Code is as follows:  

SELECT *
FROM 'table' AS t1 JOIN (select round (RAND () * (select max (id) FROM 'table') AS id) AS t2
WHERE t1.id> = t2.id
Order by t1.id asc limit 5;

However, five consecutive records are obtained. The solution can only be one query at a time and five queries at a time, but this does not meet my requirements. I need to find several

Efficient writing

The Code is as follows:  

SELECT * FROM user WHERE userId> = (select max (userId) FROM user)-(select min (userId) FROM user) * RAND () + (select min (userId) FROM user) LIMIT 5

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.