简体   繁体   中英

tally/count mysql results per day

Lets say I have a mysql table called ' signups ' with the following values:

Name            Signup Date
dog            2008-05-14 18:53:30
cat            2008-05-14 12:13:20
mouse          2008-05-14 08:51:32
giraffe        2008-05-15 22:13:31
Moose          2008-05-16 13:20:30
monkey         2008-05-16 08:51:32
mongoose       2008-05-16 22:13:31
fish           2008-05-16 13:00:30

I want to generate a report for how many animals signed up for each DAY (I don't care about the time of day). So the end result of what I'm looking for from the above example table is:

Date              Signups
2008-05-14         3
2008-05-15         1
2008-05-16         4

Is there a way to do this in mysql, or do I need to involve another language like PHP to calculate totals?

Any ideas are appreciated, thanks

SELECT  DATE(Signup_Date) AS `Date`
        , COUNT(*) AS Signups 
FROM    `signups` 
GROUP BY 
        DATE(Signup_Date)

will give you exactly what you're after.

drop table if exists users;
create table users
(
user_id int unsigned not null auto_increment primary key,
username varchar(32) unique not null,
created_date datetime not null
)
engine=innodb;

drop table if exists user_signup_summary;
create table user_signup_summary
(
signup_date date not null primary key,
counter int unsigned not null default 0
)
engine=innodb;

delimiter #

create trigger users_before_ins_trig before insert on users
for each row
begin
 insert into user_signup_summary (signup_date, counter) values (new.created_date, 1)
  on duplicate key update counter=counter+1;
end#

delimiter ;

insert into users (username, created_date) values
('f00', now()), ('bar', now()),
('alpha', now() - interval 1 day), ('beta', now() - interval 1 day),
('gamma', now() - interval 2 day);


select * from users;
select * from user_signup_summary;

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