Select temp, DATE_FORMAT(temp,'%b-%d') from test
where temp between
concat(year(now()),'-',month(now()) -3,'-', DATE_FORMAT(now(), '%d'))
and
(curdate());
我使用此查询获取3个月的数据,包括每周7天,但我没有按预期获得结果,如何通过此查询获取每周5天的数据并忽略周末来自数据库的数据。
答案 0 :(得分:6)
您可以使用DAYOFWEEK
函数,该函数返回的值范围从1
(星期日)到7
(星期六):
SELECT temp, DATE_FORMAT(temp,'%b-%d') FROM test
WHERE
DAYOFWEEK(temp) BETWEEN 2 AND 6
AND
temp BETWEEN
CONCAT(YEAR(NOW()),'-',MONTH(NOW())-3,'-',DATE_FORMAT(NOW(), '%d'))
AND
(CURDATE());
请注意,您选择过去3个月的数据的条件看起来有点可疑。做一些比较简单的事情可能更简单:
SELECT temp, DATE_FORMAT(temp,'%b-%d') FROM test
WHERE
DAYOFWEEK(temp) BETWEEN 2 AND 6
AND
temp >= NOW()-INTERVAL 3 MONTH;