Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse the order of the result set in SQLite

Tags:

select

sqlite

This could be a very basic problem, but I sort of don't know SQL at all.

To simplify the problem, let there be a table with only one column, so I can write down the rows horizontally:

1 3 11 39 41 45 52 63 66 70 72 80 81 90 92 97

As you can see, they are sorted. Now, I know there is the row with "70", and I want to query five rows before it in ascending order. That is, I want

41 45 52 63 66

How should I do the query?

I can query the five rows in descending order and reverse the result set afterwards, but I think there should be a better way.

like image 689
BlueWanderer Avatar asked May 10 '12 08:05

BlueWanderer


1 Answers

Below is the query that I was able to come up with, but I am not sure whether this is what you require and moreover the result is in descending order.

I created a table, SAMPLE, with index1 as its only column:

SELECT * FROM SAMPLE WHERE index1 < 70 ORDER BY index1 DESC LIMIT 0,5;
like image 164
likeToCode Avatar answered Nov 13 '22 08:11

likeToCode