SQLite 查询 - 出现多次并按出现次数排序的单个记录的简短列表? [英] SQLite Query - A short list of individual records which appear multiple times and ordered by the number of times they occur?

查看:74
本文介绍了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:

  1. 苹果(出现 4 次)
  2. 香蕉(出现 3 次)
  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆