SQL query that returns all dates not used in a table

后端 未结 4 1028
一个人的身影
一个人的身影 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:51

    The way to extract rows from the mysql database is via SELECT. Thus you cannot select rows that do not exist.

    What I would do is fill my blog table with all possible dates (for a year, then repeat the process)

     create table blog (
        thedate date not null,
        thetext text null,
        primary key (thedate));
    

    doing a loop to create all dates entries for 2011 (using a program, eg $mydate is the date you want to insert)

      insert IGNORE into blog (thedate,thetext) values ($mydate, null);
    

    (the IGNORE keyword to not create an error (thedate is a primary key) if thedate exists already).
    Then you insert the values normally

      insert into blog (thedate,thetext) values ($mydate, "newtext") 
      on duplicate key update thetext="newtext";
    

    Finally to select empty entries, you just have to

      select thedate from blog where thetext is null;
    

提交回复
热议问题