tags:

views:

111

answers:

6
+4  Q: 

Ordering and limit

In a table containing cities I want to get the five biggest cities and order them by name:

SELECT * FROM cities ORDER BY population DESC LIMIT 5

That gets me the biggest cities ordered by population, but I want the same cities ordered by name. Is there an easy way to do this (without turning to a subquery or sorting the cities afterwards with PHP)?

Thanks.

+1  A: 
SELECT * FROM cities ORDER BY population desc, name LIMIT 5
RedFilter
It should, please post some sample data and your output.
RedFilter
It gets the five biggest cities, but sorts them by population, not name (unless the top 5 happen to have the exact same population which is highly unlikely).
Jackson Miller
Oh, I see. I misunderstood what you were asking for.
RedFilter
A: 

SELECT * FROM cities ORDER BY population DESC, name LIMIT 5

Did you try this? I think this can work

Karaziox
A: 

Simply do

SELECT y.*
  FROM (SELECT name FROM cities ORDER BY population DESC LIMIT 5) AS x,
       cities AS y
 WHERE x.name = y.name
 ORDER BY y.name

That's all there's to it.

Cheers.

aefxx
Ah, ok now I know what you want to achieve:You will need a JOIN, like so:SELECT y.* FROM (SELECT name FROM cities ORDER BY population DESC) AS x, cities AS y WHERE x.name = y.name ORDER BY x.name
aefxx
A: 

You are going to need a subquery:

SELECT a.* 
FROM (
    SELECT * 
    FROM cities 
    ORDER BY population DESC 
    LIMIT 5
) a 
ORDER BY name;

EDIT: Just saw that you don't want a subquery. Why not? That is an efficient query that will return very quickly (there should be an index on population with or without a subquery).

Jackson Miller
It's not that I don't want a subquery. It just that I thought there was some obvious solution that I somehow didn't see.
Jonas
A: 
mysql> create temporary table temp ( ID int );
mysql> insert into temp select ID from cities order by population desc limit 5;
mysql> select a.* from cities a,temp b where a.ID=b.ID order by Name;

Temporary tables are dropped when the connection is closed, or they can be dropped manually. Temporary tables cannot be seen from other connections. The normal way would be (but it is unsupported yet):

mysql> select * from cities where ID in (select ID from cities order by population desc limit 5) order by Name;

But the answer is:

ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

(Tried with 5.0.5)

Notinlist
ggiroux said which works with 5.0.5: ( SELECT * FROM cities ORDER BY population DESC LIMIT 5 ) ORDER BY name;Give him a point! :-)
Notinlist
+7  A: 

I think what you want is this:

( SELECT * FROM cities ORDER BY population DESC LIMIT 5 ) ORDER BY name;

ggiroux
Works for me with 5.0.5
Notinlist
Simplest solution so far!
Jonas
That's elegant. I like it.
Jackson Miller
This is really internally run the same as the subquery, its not valid outside of mysql. But its nice and short.
MindStalker
@MindStalker: not a subquery, it's an undocumented extension to MySQL's UNION syntax, believe it or not. Look at the explain plan - no derived table. See http://dev.mysql.com/doc/refman/5.0/en/union.html.
ggiroux