Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get a few pieces of data randomly by mysql

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Mysql how to randomly get a few pieces of data, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Mysql randomly obtains several pieces of data: 1, through "SELECT * FROM tablename ORDER BY RAND () LIMIT" to obtain data; 2, through "ORDER BY t1.id ASC LIMIT" to obtain the specified number of data, and so on.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer.

How does mysql get several pieces of data at random?

The method of randomly obtaining several pieces of data by mysql

There are several ways to write sql statements:

The number of data items that 1:SELECT * FROM tablename ORDER BY RAND () LIMIT wants to obtain

2:SELECT * FROM `table` WHERE id > = (SELECT FLOOR (MAX (id) * RAND ()) FROM `table`) the number of data items that ORDER BY id LIMIT wants to obtain

3:SELECT * FROM `table` AS T1 JOIN (SELECT ROUND (RAND () * (SELECT MAX (id) FROM `table`) AS id) AS T2 WHERE t1.id > = t2.id

The number of data items that ORDER BY t1.id ASC LIMIT wants to obtain

4:SELECT * FROM `table`where id > = (SELECT floor (RAND () * (SELECT MAX (id) FROM `table`) the number of data items that ORDER BY id LIMIT wants to obtain

5:SELECT * FROM `table` WHERE id > = (SELECT floor (RAND ()) * ((SELECT MAX (id) FROM `table`)-(SELECT MIN (id) FROM `table`) + (SELECT MIN (id) FROM `table`) the number of data entries that ORDER BY id LIMIT wants to obtain

6:SELECT * FROM `table` AS T1 JOIN (SELECT ROUND (RAND () * ((SELECT MAX (id) FROM `table`)-(SELECT MIN (id) FROM `table`)) + (SELECT MIN (id) FROM `table`) AS id) AS T2 WHERE t1.id > = the number of data items t2.id ORDER BY t1.id LIMIT wants to obtain

Feedback:

According to the measured 380000 data, 10 items 1 can be randomly selected, and the execution time is 1-2 seconds. 2 ran for more than 20 seconds without response, tried 3 times. (3) the 10 pieces of data taken are continuous ID and executed for 0.001 seconds. 4Ten items were randomly selected, and the id was between 1 and 4000, executed for 0.003 seconds, and tried more than 10 times. 5Ten items were randomly selected, and the id was between 1 and 4000, executed for 0.003 seconds, and tried more than 10 times. The 10 pieces of data taken by 6 are continuous ID and executed for 0.001 seconds.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report