简体   繁体   English

MySql-一组时间戳数据(时间戳,事件),每天计算事件

[英]MySql - set of time stamped data (timestamp,event) calculating events per day

I have a table: id, datetime, event 我有一张桌子:ID,日期时间,事件

i also have table dates: date (Ymd format) 我也有表格日期:日期(Ymd格式)

the problem is some days don't have any events, I would like them to show 0 (or null) 问题是有些日子没有任何事件,我希望它们显示0(或null)

SELECT DATE_FORMAT(table.timestamp, '%Y-%m-%d') ydm, count(table.fkUID) FROM `table`  where table.fkUID=$var group by ydm;

is there some way to join or use conditional statements to make the result show: 有什么方法可以联接或使用条件语句来显示结果:

date|count
----------
2010-05-23| 5
2010-05-24| 0  <--- this line just doesn't exist in my query.
2010-05-26| 3
select d.date,count(e.timestamp)
from dates d
left join events e on d.date=DATE_FORMAT(e.timestamp,'%Y-%m-%d')
group by d.date;

I don't have environment for testing, and I'm not sure that I am clear about the question, but here it is my guess. 我没有测试环境,我不确定这个问题是否明确,但这是我的猜测。

The accepted wisdom seems to be that you have to create a table containing a list of all the dates you're interested in and then do an outer join on with the view you have there. 公认的智慧似乎是,您必须创建一个包含您感兴趣的所有日期的列表的表,然后对那里的视图进行外部联接。 Sucks, I know. 糟透了,我知道。

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

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