简体   繁体   中英

How to select random row from each group in MySQL?

How to randomly get one row from each group in MySQL? for ex:

A 145 <br/>
A 589***********GROUP 1 <br/>
A 576 <br/>

B 246************GROUP 2 <br/>
B 456 <br/>

C 558 <br/>
C 478***********GROUP 3 <br/>
C 4756 <br/>

want to get any row from each group.Thanks in advance :)

ORDER BY rand() calculates a random value at every read for every row. It is better to calculate a random sortorder once a day (or once an hour) for every row and store it in the detailed table, and for selecting the value just produce a temp-table with a compare-randomvalue for every category.

I just posted a solution in Combining queries for speed .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM