php – MySQL从日期开始的当前和上一个月的第一天和最后一天(没有时间戳)

前端之家收集整理的这篇文章主要介绍了php – MySQL从日期开始的当前和上一个月的第一天和最后一天(没有时间戳)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望以下查询将给你的想法我正在寻找 –
SELECT SUM(t1.hours) AS totalhours FROM
(
    SELECT (time_to_sec(timediff(time_out,time_in)) / 3600) AS hours FROM bb_work_log 
    WHERE user_id = 6 AND (working_date BETWEEN '2014-04-01' AND '2014-04-31')
) AS t1

在我的查询中,您可以看到我手动给出的working_date.但是,我不想手动做.我想动态地选择当月的第一天和最后一天.

您可以使用LAST_DAY(NOW() – INTERVAL 1 MONTH)INTERVAL 1 DAY,将从现在起减去一个月,并在上个月的LAST_DAY中加1天将会提供当月的第一天
SELECT SUM(t1.hours) AS totalhours FROM
(
    SELECT (time_to_sec(timediff(time_out,time_in)) / 3600) AS hours FROM bb_work_log 
    WHERE user_id = 6 
    AND (working_date BETWEEN LAST_DAY(NOW() - INTERVAL 1 MONTH) 
    AND LAST_DAY(NOW()))
) AS t1

LAST_DAY(NOW() – INTERVAL 1 MONTH) this will give you the last day of
prevIoUs month

First/Last day of Month Fiddle Demo

原文链接:https://www.f2er.com/php/130357.html

猜你在找的PHP相关文章