duancan2539 2015-11-15 18:25
浏览 106
已采纳

MySQL选择上个月的行,但它包含(31/30/28天)或闰年

Now, I have a mysql query to get the rows from last month where date is the timestamp column like,

date BETWEEN date_format(NOW() - INTERVAL 1 MONTH, '%Y-%m-01')
AND last_day(NOW() - INTERVAL 1 MONTH);

But the problem is, I am getting only the records of 30 days of the last month. Its working fine for the months with 30 days or lower. But for the months like December I need all the records of 31 days. What will be the correction here? Thanks.

  • 写回答

3条回答 默认 最新

  • dqnz43863 2015-11-15 19:53
    关注

    I have tried many references & at last I have got my exact answer. My final code is,

    EXTRACT(YEAR_MONTH FROM date) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL 1 MONTH)
    

    Thanks everyone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题