简体   繁体   中英

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.

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.

Having a problem getting started with the code. I assume I can do this with MySQL count or something.

you can using mysql function are "GROUP BY

select count(id) from table group by year

You can use MySQL COUNT() function with GROUP BY like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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