SQLite 查询 - 出现多次并按出现次数排序的单个记录的简短列表? [英] SQLite Query - A short list of individual records which appear multiple times and ordered by the number of times they occur?
本文介绍了SQLite 查询 - 出现多次并按出现次数排序的单个记录的简短列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个包含以下记录的表:
Say I have a table with the following records:
ID | 类型 | 项目 |
---|---|---|
1 | 食物 | 苹果 |
2 | 食物 | 苹果 |
3 | 食物 | 橙色 |
4 | 食物 | 苹果 |
5 | 食物 | 香蕉 |
6 | 食物 | 香蕉 |
7 | 喝 | 可乐 |
8 | 喝 | 水 |
9 | 食物 | 香蕉 |
10 | 食物 | 苹果 |
如何构造一个查询,该查询将返回单列项目列表,这些项目仅属于 Type:food,并且仅包含在表中多次出现的每个项目的一次出现 - 最后按以下顺序排序它们在表格中出现的次数?
How do I construct a query that will return a single column list of Items, which are only of the Type:food, and which just contains one occurrence of each item which appears multiple times in the table - and finally are ordered by the number of times they appear in the table?
因此上表的结果将是以下列表:
So the result for the above table would be the following list:
- 苹果(出现 4 次)
- 香蕉(出现 3 次)
- 橙色(出现 1 次)
谢谢.
推荐答案
试试这个:
select Item, count(Item) as COUNT
FROM TableName
WHERE Type='food'
GROUP BY Item
ORDER BY COUNT DESC
结果:
ITEM COUNT
apple 4
banana 3
orange 1
在 SQL Fiddle 中查看结果.
See result in SQL Fiddle.
这篇关于SQLite 查询 - 出现多次并按出现次数排序的单个记录的简短列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文