前:
month(log_date)=month(now());
month(log_date) = 12;
输出 5个星期日
答案 0 :(得分:1)
USW BETWEEN : - BETWEEN运算符选择给定范围内的值。
注意: - DAYOFWEEK实际上为星期日返回1
SELECT count(*) AS total_sunday FROM `table` WHERE DAYOFWEEK(`date`) = 1 BETWEEN '2017-11-01' AND '2017-11-30';
答案 1 :(得分:1)
可能的方法,避免使用任何表。这会查找当月的天数,并且根据该天数以及最后一天的星期几,它只返回一个值
SELECT CASE DAYOFMONTH(LAST_DAY(NOW()))
WHEN 31 THEN
CASE DAYOFWEEK(LAST_DAY(NOW()))
WHEN 1 THEN 5
WHEN 2 THEN 5
WHEN 3 THEN 5
ELSE 4
END
WHEN 30 THEN
CASE DAYOFWEEK(LAST_DAY(NOW()))
WHEN 1 THEN 5
WHEN 2 THEN 5
ELSE 4
END
WHEN 29 THEN
CASE DAYOFWEEK(LAST_DAY(NOW()))
WHEN 1 THEN 5
ELSE 4
END
ELSE 4
END
答案 2 :(得分:0)
在mysql中,只需复制并粘贴下面给出的查询即可。它将为您提供当月的星期天数。
WITH RECURSIVE offdays as(
SELECT
LAST_DAY(CURDATE()-INTERVAL 1 MONTH) + INTERVAL 1 DAY AS `Date`,
DAYNAME(LAST_DAY(CURDATE()-INTERVAL 1 MONTH) + INTERVAL 1 DAY) AS `DayName`
UNION ALL
SELECT `Date` + INTERVAL 1 DAY, DAYNAME(`Date` + INTERVAL 1 DAY)
FROM offdays WHERE `DATE` < LAST_DAY(CURDATE())
) SELECT count(*) FROM offdays where DAYNAME(DATE) = 'Sunday';
您将获得当月的星期日数。只需将CURDATE()替换为任何日期即可。该查询将为您提供所提供日期的月份中的周日数。