Retrieve missing dates from database via MySQL

后端 未结 2 1660
感情败类
感情败类 2021-01-23 00:43

So I have a table where I collect data for the jobs that I do. Each time I create a job I assign it a date. The problem with this is the days I don\'t have jobs aren\'t stored i

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 01:18

    One idea is that you could have a table with all of the dates in it that you want to show and then do an outer join with that table.

    So if you had a table called alldates with one column (job_data_date):

    SELECT ad.job_data_date, SUM(job_data_invoice_amount) as job_data_date_income 
    FROM alldates ad left outer join job_data jd on ad.job_data_date = jd.job_data_date 
    WHERE ad.job_data_date >= '2010-05-05' 
    GROUP BY ad.job_data_date 
    ORDER BY ad.job_data_date;
    

    The down side is that you would need to keep this table populated with all of the dates you want to show.

提交回复
热议问题