Get a list of dates between two dates

后端 未结 20 2101
醉梦人生
醉梦人生 2020-11-22 00:26

Using standard mysql functions is there a way to write a query that will return a list of days between two dates.

eg given 2009-01-01 and 2009-01-13 it would return

20条回答
  •  被撕碎了的回忆
    2020-11-22 00:57

    I am using Server version: 5.7.11-log MySQL Community Server (GPL)

    Now we will solve this in a simple way.

    I have created a table named "datetable"

    mysql> describe datetable;
    +---------+---------+------+-----+---------+-------+
    | Field   | Type    | Null | Key | Default | Extra |
    +---------+---------+------+-----+---------+-------+
    | colid   | int(11) | NO   | PRI | NULL    |       |
    | coldate | date    | YES  |     | NULL    |       |
    +---------+---------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    

    now, wee will see the inserted records within.

    mysql> select * from datetable;
    +-------+------------+
    | colid | coldate    |
    +-------+------------+
    |   101 | 2015-01-01 |
    |   102 | 2015-05-01 |
    |   103 | 2016-01-01 |
    +-------+------------+
    3 rows in set (0.00 sec)
    

    and here our query to fetch records within two dates rather than those dates.

    mysql> select * from datetable where coldate > '2015-01-01' and coldate < '2016-01-01';
    +-------+------------+
    | colid | coldate    |
    +-------+------------+
    |   102 | 2015-05-01 |
    +-------+------------+
    1 row in set (0.00 sec)
    

    hope this would help many ones.

提交回复
热议问题