doubi4435 2015-04-02 18:53
浏览 39

列出没有数据库数据的月份

The current query work perfect the problem is there is no data for the current month (April) so it does not list the month as 0, How do I get it to list the month as having 0 interivews?

QUERY

select distinct from_unixtime(interv_date,'%M %Y') AS month, count(*) as totals
from " . TABLE_PREFIX . "interviews
group by month
order by interv_date desc
limit 6

NOTE that the interv_date column is a epoch timestamp (example: 1428003691)

PHP

<table class=\"highchart\" data-graph-container=\"#graphcontainer\" data-graph-type=\"column\" data-graph-legend-disabled=\"1\">
<caption>Interviews by Month</caption>
<thead>
<tr>
<th>Month</th>
<th>Totals</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < $stat_logs["count"]; $i++) {
    echo "<tr>
";
    echo "<td>" . $stat_logs[$i]["month"] . "</td>
";
    echo "<td>" . $stat_logs[$i]["totals"] . "</td>
";
    echo "</tr>
";
}
?>
echo "</tbody>
echo "</table>

OUTPUT

Month           Totals
March 2015      7
February 2015   5
January 2015    12
December 2014   18
November 2014   19
October 2014    5

I need it to output something like this:

Month           Totals
April 2015      0
March 2015      7
February 2015   5
January 2015    12
December 2014   18
November 2014   19

Take not that not every month will have interviews so it won't just be the current month (first month) Any help on how I can get the months with no interviews to show up on the list would be great.

  • 写回答

1条回答 默认 最新

  • doutonghang2761 2015-04-03 01:49
    关注

    I've been working on this WAAAAAYYYY too long. But here's a start... http://sqlfiddle.com/#!9/23389/2

    It works perfectly well but it's a bit clunky, with two temp tables. No hardcoding dates, though! The tables should actually be temp tables, although sqlfiddle didn't work with temp tables so I made them real tables.

    -- Get a table with the months of interest in it
    SET @RightNow = curdate();
    create table LastSixMonths (dateVal datetime);
    Insert into LastSixMonths values(@RightNow);
    Insert into LastSixMonths values(@RightNow - INTERVAL 1 MONTH);
    Insert into LastSixMonths values(@RightNow - INTERVAL 2 MONTH);
    Insert into LastSixMonths values(@RightNow - INTERVAL 3 MONTH);
    Insert into LastSixMonths values(@RightNow - INTERVAL 4 MONTH);
    Insert into LastSixMonths values(@RightNow - INTERVAL 5 MONTH);
    
    -- Summarize the recent interviews
    create table Totals (monthAndYear varchar (20), interviews int);
    Insert into Totals
    select distinct from_unixtime(intv.interv_date,'%M %Y') as monthAndYear, 
                    count(*) 
    from interviews intv 
    group by from_unixtime(intv.interv_date,'%M %Y')
    order by interv_date desc
    limit 6
    
    -- Do a left join on recent months and summarized interviews
    select date_format(six.dateval, '%M %Y'), 
        IF (interviews IS NULL, 0, Interviews) as totalInterviews
    from LastSixMonths six 
    left join Totals tot 
        on date_format(six.dateval, '%M %Y') = tot.monthAndYear
    order by six.dateval desc
    limit 6;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭