简体   繁体   中英

How do I get the max id from the first n id's in a MySQL table?

I am trying to get the max id of the first n id's in a MySQL database table where the ids are not necessarily sequential. The first n id's are determined by ordering by id ascending. I am using the following query, but this returns the max id in the entire table.

SELECT MAX( id )
FROM files
ORDER BY id ASC
LIMIT 8750000

What am I doing wrong, or ... how do I do this?

SELECT MAX(t.id) FROM
(SELECT id FROM files order by id ASC limit <n>) AS t ;

Of course you will need to replace <n> with an actual value you need.

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