简体   繁体   English

MySQL选择日期,加入统计数据-如何?

[英]MySQL select date, join statistic data - how?

I have some statistics I need to report on a monthly basis. 我有一些统计数据需要每月报告。 I need my query to return 0's for statistics which aren't there. 我需要查询返回不存在的统计信息的0。 The only way I know to do this is to use SQL to generate a list of days within the specified time period and LEFT JOIN the statistic data that I need to report on. 我知道做到这一点的唯一方法是使用SQL生成指定时间段内的天数列表,并LEFT JOIN我需要报告的统计数据。

Problem is.... I've never not selected from a table like that... how do I do that? 问题是...。我从来没有从这样的表中选择过...我该怎么做?

Compare your returned values, for statistic that never existed your returned value probably will be null, all you need to compare returned value with null and if null return 0 otherwise return real result 比较您的返回值,对于不存在的统计信息,您的返回值可能为null,您需要将返回值与null进行比较,如果null返回0,否则返回实际结果

select s.Name , IFNULL(d.Value, 0) as Value
 from Statustic s
LEFT JOIN t_Data d on (d.Stat_ID=s.ID)

and you can use this link to read more at http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_isnull 您可以使用此链接在http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_isnull阅读更多内容

如果值为NULL,请使用

SELECT IFNULL( statistic, 0 ) AS statistic FROM myTable;

I ended up just using SQL to pull the data I have for the given period and handling everything else on the PHP side. 我最终只是使用SQL提取给定时间段内的数据,并处理了PHP方面的所有其他信息。 Not exactly what I wanted, but it works. 不完全是我想要的,但是它可以工作。

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

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