简体   繁体   English

每天计算/计算mysql结果

[英]tally/count mysql results per day

Lets say I have a mysql table called ' signups ' with the following values: 假设我有一个名为' signups '的mysql表,其中包含以下值:

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? 有没有办法在mysql中执行此操作,还是需要使用其他语言(如PHP)来计算总计?

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;

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

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