简体   繁体   English

如何根据列值对行进行计数?

[英]How to count rows based on column value?

Using PHP and MySQL, I have a table that holds check in data for customers: the month and year they checked in. 使用PHP和MySQL,我有一个表,用于保存客户的签到数据:他们签到的月份和年份。

ID  Month   Year

1   01      2015
1   01      2015
4   01      2015
1   03      2015
4   03      2015
1   05      2015

I need a total count of rows per month and year. 我需要每月和每年的总行数。 So in this example I should get a total of 3 for id 1 for the month of 01 and year 2015. I'm adding the total days a person checked in for each month of each year. 因此,在此示例中,我应该在01年和2015年的ID为1的总数中获得3。我将添加一个人每年每年每个月的总入住天数。

Having a problem getting started with the code. 开始使用代码时遇到问题。 I assume I can do this with MySQL count or something. 我以为我可以用MySQL count或其他方式做到这一点。

you can using mysql function are "GROUP BY 您可以使用mysql函数的“ GROUP BY

select count(id) from table group by year 按年份从表组中选择count(id)

You can use MySQL COUNT() function with GROUP BY like: 您可以对GROUP BY使用MySQL COUNT()函数,例如:

  SELECT ID, Month, Year, COUNT(*)
    FROM checked_customer
    GROUP BY ID, Month, Year;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM