订购和限制

发布于 2024-08-20 05:22:48 字数 199 浏览 3 评论 0原文

在包含城市的表中,我想要获取五个最大的城市并按名称排序:

SELECT * FROM cities ORDER BY population DESC LIMIT 5

这使我获得按人口排序的最大城市,但我想要按名称排序相同的城市。有没有一种简单的方法可以做到这一点(无需转向子查询或随后使用 PHP 对城市进行排序)?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪我鬧 2024-08-27 05:22:48

我想你想要的是这样的:

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

I think what you want is this:

( SELECT * FROM cities ORDER BY population DESC LIMIT 5 ) ORDER BY name;
裸钻 2024-08-27 05:22:48
SELECT * FROM cities ORDER BY population desc, name LIMIT 5
SELECT * FROM cities ORDER BY population desc, name LIMIT 5
也只是曾经 2024-08-27 05:22:48

您将需要一个子查询:

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

编辑:刚刚看到您不需要子查询。为什么不呢?这是一个高效的查询,返回速度非常快(应该有一个带有或不带有子查询的人口索引)。

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).

鹿港巷口少年归 2024-08-27 05:22:48

简单地做

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

这就是全部。

干杯。

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.

站稳脚跟 2024-08-27 05:22:48
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;

连接关闭时会删除临时表,也可以手动删除临时表。从其他连接中无法看到临时表。
正常的方法是(但还不支持):

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

但答案是:(

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

尝试过 5.0.5)

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)

妥活 2024-08-27 05:22:48
SELECT * FROM cities ORDER BY population DESC, name LIMIT 5

你尝试过这个吗?我认为这可行

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

Did you try this? I think this can work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文