SQL重新排序查询结果

huangapple go评论58阅读模式
英文:

SQL Re-sorting the result of a query

问题

Looking for some help, to re-order the result of a query.

So I have a table that contains just two columns ID, Tag.

Sample data:

1, France
2, Germany
3, Spain
4, USA
5, Spain
... and so on

I use a query to pull out the five most popular tags:

SELECT COUNT(ItemID) as tagcount, tag
FROM TagTable
GROUP BY Tag
ORDER BY tagcount DESC
LIMIT 0, 5

This works well and gives an output, for example:

49, France
47, USA
42, Italy
39, England
38, Portugal

However, after plucking the "top 5," I'd like the output to be alphabetical, eg:

39, England
49, France
42, Italy
38, Portugal
47, USA

Is this possible and if so, how?

英文:

Looking for some help, to re-order the result of a query.

So I have a table that contains just two columns ID, Tag.

Sample data:

1, France
2, Germany
3, Spain
4, USA
5, Spain
... and so on

I use a query to pull out the five most popular tags:

SELECT COUNT(ItemID) as tagcount, tag
FROM TagTable
GROUP BY Tag
order by tagcount desc
limit 0, 5

This works well and gives an output, for example:

49, France
47, USA
42, Italy
39, England
38, Portugal

However, after plucking the "top 5", I'd like the output to be alphabetical, eg:

39, England
49, France
42, Italy
38, Portugal
47, USA

Is this possible and if so, how?

Thanks in advance.

答案1

得分: 1

Wrap it into one more select:

select * from (
    SELECT COUNT(ItemID) as tagcount, tag
    FROM TagTable
    GROUP BY Tag
    order by tagcount desc
    limit 0,5
) t1
order by tag
英文:

Wrap it into one more select:

select * from (
    SELECT COUNT( ItemID) as tagcount, tag
    FROM TagTable
    GROUP BY Tag
    order by tagcount desc
    limit 0,5
) t1
order by tag

huangapple
  • 本文由 发表于 2023年4月13日 23:57:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定