Yura  Borisenko Yura Borisenko - 3 months ago 20
MySQL Question

How can I select rows in reverse order (mysql)

How can I select rows in reverse order (DB MySQL)?

For example,
I have a table with 12 rows (fields: id,location), I want select -4th row before a row with id = 6,
i.e. wanted row will have id = 'not necessarily 2',
but there is condition - where table.location='some_location'.


What should the content of request to mysql be like?





Editted at 30 minut later.

Here is solution!
Some example, I checked drodil's suggestion so:

mysql> select * from subscrs where id < 100000 order by id desc limit 4;
+-------+--------+-----------+-------+
| uid | subscr | event | id |
+-------+--------+-----------+-------+
| 5307 | 5123 | feed_news | 99999 |
| 25985 | 5211 | feed_news | 99998 |
| 15123 | 130 | feed_news | 99997 |
| 28368 | 19497 | feed_news | 99996 |
+-------+--------+-----------+-------+
4 rows in set (0.00 sec)


Drodil, thank you!

Answer

Or you could do it without caring the deleted results, just get the fourth before the given id (6) by:

SELECT * FROM SomeTable WHERE id < 6 ORDER BY id DESC LIMIT 4,1