简体   繁体   中英

How do I find next row in Postgres based on previous Id?

Suppose I have a table like this:

Id  Name
----------
1   John
2   Mary
5   Jason
6   Amit
7   Daniel
10  Emma
11  Max
15  Sachin

I'm trying to create a query where I will pass one id and get the next row from the table.

You can filter on id s that are greater than your parameter, order by id and retain the first row only, using limit :

select t.* from mytable t where id > ? order by id limit 1

This gives you the record with the next higher id to the parameter.

You can also use this

select * from table_name where id = (select min(id) from table_name where id > {Id})

This will return you the next available row present in the table

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