How to find missing rows (dates) in a mysql table?

前端 未结 3 1909
故里飘歌
故里飘歌 2020-12-11 07:45

I have tried several topics like this one: How to find missing data rows using SQL? here, but I couldn\'t make it work in my situation.

I have a table named po

3条回答
  •  自闭症患者
    2020-12-11 08:35

    The simplest way to find missing dates is to use a calendar table. I've posted code to create and populate a calendar table for PostgreSQL; you should be able to adapt it without any trouble.

    With the calendar table in place, your query is pretty simple, and easy to understand. To find the missing dates for October, 2011, you'd use something along these lines. (Guessing at your "posts" table.)

    select c.cal_date
    from calendar c
    left join posts p on (c.cal_date = p.date)
    where p.date is null
      and c.cal_date between '2011-10-01' and '2011-10-31'
      and p.userid = 1
    order by c.cal_date
    

提交回复
热议问题