mysql select dates without tables

后端 未结 3 1259
长发绾君心
长发绾君心 2021-01-04 14:10

I simply need to return a list of all days within a month. I\'m not accessing a specific table. So I need a sql select statement if given the month of February, return the

3条回答
  •  情话喂你
    2021-01-04 14:34

    I agree with the comments, that something like this shouldn't be done in the database, but technically its possible. If you give the start and end date, adding additional numbers to the subquery if necessary:

    SELECT '2011-02-01' + INTERVAL a + b DAY dte
    FROM
     (SELECT 0 a UNION SELECT 1 a UNION SELECT 2 UNION SELECT 3
        UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
        UNION SELECT 8 UNION SELECT 9 ) d,
     (SELECT 0 b UNION SELECT 10 UNION SELECT 20 
        UNION SELECT 30 UNION SELECT 40) m
    WHERE '2011-02-01' + INTERVAL a + b DAY  <  '2011-03-01'
    ORDER BY a + b
    

    Results:

    "2011-02-01"
    "2011-02-02"
    "2011-02-03"
    ....
    "2011-02-28"
    

提交回复
热议问题