Php 如何计算SQL中每个值的表行数?

Php 如何计算SQL中每个值的表行数?,php,mysql,sql,Php,Mysql,Sql,我是新来的,所以如果它不是固定的主题,请修复我。 我有这张桌子: id | uid | month | year ----------------------- 1 | 5 | 12 | 2018 2 | 5 | 12 | 2018 3 | 9 | 12 | 2018 4 | 3 | 01 | 2019 我想计算每个uid的month和year值等于-month=12和year=2018的次数 例如,我希望得到这样的响应: uid=5 - count = 2 uid=9 - count = 1

我是新来的,所以如果它不是固定的主题,请修复我。 我有这张桌子:

id | uid | month | year
-----------------------
1 | 5 | 12 | 2018
2 | 5 | 12 | 2018
3 | 9 | 12 | 2018
4 | 3 | 01 | 2019
我想计算每个
uid
month
year
值等于-
month=12
year=2018的次数

例如,我希望得到这样的响应:

uid=5 - count = 2
uid=9 - count = 1

怎么可能呢?

使用
按uid分组

select uid, count(*) as counter
from table
where month = 12 and year = 2018
group by uid 

您可以使用条件聚合:

select uid,
       sum(case when year = 2018 and month = 12 then 1 else 0 end) as cnt
from t
group by uid;
这将包括计数为0的
uid
s。如果您只希望UID的计数大于0,请使用
where
子句:

select uid, count(*)
from t
where year = 2018 and month = 12
group by uid;

从表中选择uid,count(*),其中year=2018和month=12按uid分组

Hi,首先感谢您的回答。我尝试按计数对结果排序,下面是我添加到查询中的内容:
orderbycount(*)DESC
有什么问题吗?哦,对不起,我的错-没有orderby就可以了。谢谢你最好的回答!订单很好,有什么问题吗?请看我上一篇评论。很好,你发布时我正在键入。可能是重复的