I have PostgreSQL table:
Username1 SomeBytes1
Username2 SomeBytes1
Username1 SomeBytes1
Username1 SomeBytes1
I need to get some rows from with name Username1 but from the end of the table. For example i need last to rows with Username1
select from my_table where user = Username1 LIMIT 2
Gives me first 2 rows, but i need last two.
How can i select it?
Thank you.
first and last in a table is very arbitrary. In order to have a good predictable result you should always have an
order byclause. And if you have that, then getting the last two rows will become easy.For instance, if you have a primary key or something like an ID (which is populated by a sequence), then you can do:
desctells the database to sort the rows in reverse order, which means that last will be first.