What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?

后端 未结 9 1902
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 06:12

I\'m building a quick csv from a mysql table with a query like:

select DATE(date),count(date) from table group by DATE(date) order by date asc;
9条回答
  •  佛祖请我去吃肉
    2020-11-22 07:08

    When I had to deal with this problem, to fill in missing dates I actually created a reference table that just contained all dates I'm interested in and joined the data table on the date field. It's crude, but it works.

    SELECT DATE(r.date),count(d.date) 
    FROM dates AS r 
    LEFT JOIN table AS d ON d.date = r.date 
    GROUP BY DATE(r.date) 
    ORDER BY r.date ASC;
    

    As for output, I'd just use SELECT INTO OUTFILE instead of generating the CSV by hand. Leaves us free from worrying about escaping special characters as well.

提交回复
热议问题