订购和限制
在包含城市的表中,我想要获取五个最大的城市并按名称排序:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我想你想要的是这样的:
I think what you want is this:
您将需要一个子查询:
编辑:刚刚看到您不需要子查询。为什么不呢?这是一个高效的查询,返回速度非常快(应该有一个带有或不带有子查询的人口索引)。
You are going to need a subquery:
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).
简单地做
这就是全部。
干杯。
Simply do
That's all there's to it.
Cheers.
连接关闭时会删除临时表,也可以手动删除临时表。从其他连接中无法看到临时表。
正常的方法是(但还不支持):
但答案是:(
尝试过 5.0.5)
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):
But the answer is:
(Tried with 5.0.5)
你尝试过这个吗?我认为这可行
Did you try this? I think this can work