SQL Count() 列值 [英] SQL Count() column values

查看:63
本文介绍了SQL Count() 列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下源表:

pid | code
----------
1      P2
2      P2
3      P3
4      P1
5      P2
6      P1

现在,我想获取每个代码存在多少次的信息:

Now, I want to get the information how many times each code exists:

code | count
------------
P2      3
P1      2
P3      1

所以我想计算代码列中的值并将其分配给一组不同的可用代码值.最后我想按计数订购.

So I want to count the values from code column and assign it to the distinct set of available code values. Finally I want to order by count number.

推荐答案

SQL小提琴:

SELECT t.code, COUNT(*) AS `count`
FROM MyTable t
GROUP BY t.code
ORDER BY COUNT(*) DESC

这篇关于SQL Count() 列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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