0

我有这个数据,在一个表中categories

id | name | post_count
 1 | A    | 10
 2 | B    | 15
 3 | C    | 8
 4 | D    | 14
 5 | E    | 1
 6 | F    | 20

我想按 获取前 4 个类别,post_count并按 对它们进行排序name

如果我做

SELECT * FROM categories ORDER BY post_count DESC LIMIT 4

我将按顺序获取类别:F, B, D, A,而我想要A, B, D, F

这可以通过单个 SQL 查询实现吗?我怎样才能做到这一点?

4

3 回答 3

3

您可以使用子查询:

SELECT * FROM (SELECT * FROM 
               categories ORDER BY 
               post_count DESC LIMIT 4) AS A
ORDER BY name 

看到这个 SQLFiddle

于 2012-10-25T12:25:49.650 回答
3
select * from (

SELECT * FROM categories ORDER BY post_count DESC LIMIT 4)a
order by name
于 2012-10-25T12:27:18.120 回答
1
select *
from (
    select *
    from categories
    order by post_count desc
    limit 4
) s
order by name
于 2012-10-25T12:26:51.173 回答