简体   繁体   中英

Is there a way to get how many rows are in my table for each month of the year?

I would like to work out how many user sign ups i have gotten in each month. I've got a users table which contains a signUpDate of type varchar in dd/mm/yyyy format.

I've written a select statement which gets how many sign ups I have gotten this month:

select COUNT(*) as numberOfSignUps from users WHERE STR_TO_DATE(signupDate,'%d/%m/%Y') >= STR_TO_DATE('31/05/2020','%d/%m/%Y')  AND STR_TO_DATE(signupDate,'%d/%m/%Y') <= STR_TO_DATE('30/06/2020','%d/%m/%Y'); 

This works fine but is there a way to go back through all previous months without manually adding the dates?

The desired result would be columns of the months with the number of sign ups for each.

You seem to want group by . I would suggest:

select year(signupDate) as year, month(signupDate) as month, COUNT(*) as numberOfSignUps
from users 
group by year, month;

Based on your comment, you should fix your data. Use the right data types!

In the meantime, just use string functions:

select right(signupDate, 7) as mmyyyy, COUNT(*) as numberOfSignUps
from users 
group by mmyyyy

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