英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论