A
A
Andrey Privalov2011-11-16 21:10:16
SQL
Andrey Privalov, 2011-11-16 21:10:16

[Solved] MySQL query?

Friends, the head does not cook, help the collective mind.
Let's assume the situation.
MySQL table with one field `id`
id
1
2
3
4
5
6
7
8
9
10
You need to select the last 5 records that are greater than 2 with the condition that the id sort is ascending. — i.e. the result should be 6,7,8,9,10

SELECT * FROM `table` WHERE id > '2' ORDER BY id ASC LIMIT 5

will obviously choose 3,4,5,6,7
SELECT * FROM `table` WHERE id > '2' ORDER BY id DESC LIMIT 5

will select the desired records, but incorrectly sorted.
How?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NikoB, 2011-11-16
@NikoB

(
SELECT *
FROM `table`
WHERE id > '2'
ORDER BY id DESC
LIMIT 5
)
ORDER BY id ASC

M
Melkij, 2011-11-16
@melkij

select `id` from (select `id` from `table` where `id`>2 order by `id` desc limit 5) as `tmptable` order by `id` desc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question