Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql select next 10 after something

Is there any way to select the next 10 rows after the row with ID where rows are ordered by something.

I've come up only with simple solution like:

  1. select all
  2. apply a loop to find ID and then return next 10

Thanks!

like image 622
typedef Avatar asked Jul 31 '26 12:07

typedef


1 Answers

You can use LIMIT . See the docs:

SELECT * from tableName
WHERE id > yourID
ORDER BY ID ASC
LIMIT NumRows

sqlfiddle demo

If the rows aren't orderd by ID, you can do:

SELECT * FROM tab1
WHERE orderColumn > (
  SELECT orderColumn from tab1
  WHERE id = YourId
)
ORDER BY orderColumn
LIMIT 10

sqlfiddle demo

like image 79
Filipe Silva Avatar answered Aug 03 '26 02:08

Filipe Silva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!