简体   繁体   中英

How do I select the 1st row from a selection of 10 random rows

Lets say I have a 1000 rows in my table.

I want to select 10 of those at random.

SELECT * FROM table ORDER BY RAND() LIMIT 10

Then I want to select the row in that result with the highest value for number

SELECT * FROM table ORDER BY number DESC LIMIT 1

Can anyone help me come up with an efficient way of doing this?

Just use a subquery:

SELECT *
FROM (
    SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1

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