dra87370 2015-05-18 10:21 采纳率: 100%
浏览 258
已采纳

MySQL - 如何显示特定月份的所有日期记录?

select a.Date 
from (
    select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a 
where a.Date between '2015-04-15' and '2015-05-15' ORDER BY Date

Above query working fine I am getting 30 records from selected date to selected date. But I want to show complete month record. If i choose February then all 28 days(29 if leap year) record will showing. Same as Mar = 31 records April = 30 records. etc.

EDIT : See screenshot. I want to show all days in a month.

enter image description here

If possible to PHP please post your answer.

  • 写回答

2条回答 默认 最新

  • dongsheng1698 2015-05-18 11:20
    关注

    If you know your input year and month then you can always set the first day, say the input is Y = 2012 M=02 ,the first day would be always 2012-02-01 and using that date you can get the last day and then the dates in that range. Something as

    select a.Date 
    from (
        select last_day('2012-02-01') - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
        from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
    ) a 
    where a.Date between '2012-02-01' and last_day('2012-02-01') order by a.Date;
    
    +------------+
    | Date       |
    +------------+
    | 2012-02-01 |
    | 2012-02-02 |
    | 2012-02-03 |
    | 2012-02-04 |
    | 2012-02-05 |
    | 2012-02-06 |
    | 2012-02-07 |
    | 2012-02-08 |
    | 2012-02-09 |
    | 2012-02-10 |
    | 2012-02-11 |
    | 2012-02-12 |
    | 2012-02-13 |
    | 2012-02-14 |
    | 2012-02-15 |
    | 2012-02-16 |
    | 2012-02-17 |
    | 2012-02-18 |
    | 2012-02-19 |
    | 2012-02-20 |
    | 2012-02-21 |
    | 2012-02-22 |
    | 2012-02-23 |
    | 2012-02-24 |
    | 2012-02-25 |
    | 2012-02-26 |
    | 2012-02-27 |
    | 2012-02-28 |
    | 2012-02-29 |
    +------------+
    29 rows in set (0.00 sec)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1