SQL query that returns all dates not used in a table

后端 未结 4 1020
一个人的身影
一个人的身影 2020-12-18 10:44

So lets say I have some records that look like:

2011-01-01 Cat
2011-01-02 Dog
2011-01-04 Horse
2011-01-06 Lion

How can I construct a query

4条回答
  •  死守一世寂寞
    2020-12-18 10:54

    You probably not going to like this:

    select '2011-01-03', count(*) from TABLE where postdate='2011-01-03' 
      having count(*)=0 union
    select '2011-01-04', count(*) from TABLE where postdate='2011-01-04' 
      having count(*)=0 union
    select '2011-01-05', count(*) from TABLE where postdate='2011-01-05' 
      having count(*)=0 union
    ... repeat for 2 weeks
    

    OR

    create a table with all days in 2011, then do a left join, like

    select a.days_2011
    from all_days_2011
    left join TABLE on a.days_2011=TABLE.postdate
    where a.days_2011 between date(now()) and date(date_add(now(), interval 2 week))
    and TABLE.postdate is null;
    

提交回复
热议问题