@H_
301_0@
我希望以下
查询将给你的想法我正在寻找 –
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