简体   繁体   中英

Can I use inner query into inner query for 3rd max or nth max id?

My query is given below:

select max(id)
from crmtestregis
where id < (select max(id) from  crmtestregis where id <
    (select max(id) from crmtestregis));

Please suggest inner query is best or some other option is best?

I think you can use LIMIT and OFFSET here:

SELECT id
FROM crmtestregis
ORDER BY id DESC
LIMIT 1
OFFSET 2;

The above query would return the third record from the top, with an ordering of id descending.

If you instead wanted the third highest unique value, then we can use DISTINCT and a similar query:

SELECT DISTINCT id
FROM crmtestregis
ORDER BY id DESC
LIMIT 1
OFFSET 2;
SELECT id FROM crmtestregis
ORDER BY id DESC LIMIT n-1,1;

Replace n with 'N' max ID (MySQL)

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