user2133404
user2133404

Reputation: 1857

order by after limiting number of rows in mysql

I have the following table

PACKAGE     DISTANCE
--------    --------
 Q1           5.0

 Q2           4.5

 Q3           0.5

I want to extract the rows in MySQL in such a way that Q2 is followed by Q1.

When I use this query

select package,distance from new_travel order by distance desc limit 0,2

I am getting Q1 followed by Q2.

When I use

 (select package,distance from new_travel order by distance desc limit 0,2) order by distance asc

its giving error. How to extract those rows in the order needed?

Upvotes: 0

Views: 53

Answers (1)

Ryoku
Ryoku

Reputation: 822

Limit in SQL means( in 0,2 for example) start on 0 and bring two. You need to bring three so do 0,3 on your limit.

Or, if you want to only bring the results Q2 and Q3 do 1,2

To get Q2 followed by Q1 all you need to do is:

select package,distance from new_travel order by distance asc limit 1,2 

Upvotes: 1

Related Questions