下面的sql工作正常,并返回90个记录按预期方式,并且计数是正确的,但是当在sql中添加日期过滤时,它只返回44行而不是90,但总计数是正确。mysql日期过滤不起作用
SQL不注日期的过滤:
sql without date filtering:
SELECT
t4.firstname,
t1.category
,COUNT(t3.catid) as days
FROM category AS t1
LEFT JOIN (SELECT
DISTINCT empid,t1.catid
FROM attendance AS t2
LEFT JOIN category AS t1 ON (t1.catid>0)
) AS t2
ON (t1.catid=t2.catid)
LEFT JOIN attendance AS t3 ON (t2.empid=t3.empid AND t3.catid=t2.catid)
left join employee t4 on t2.empId=t4.empid
GROUP BY t2.empid,t2.catid
ORDER BY t2.empid,t2.catid;
与日期过滤:
SELECT
t4.firstname,
t1.category
,COUNT(t3.catid) as days
FROM category AS t1
LEFT JOIN (SELECT
DISTINCT empid,t1.catid
FROM attendance AS t2
LEFT JOIN category AS t1 ON (t1.catid>0)
) AS t2
ON (t1.catid=t2.catid)
LEFT JOIN attendance AS t3 ON (t2.empid=t3.empid AND t3.catid=t2.catid)
left join employee t4 on t2.empId=t4.empid
where
date_format(dateIn, '%Y-%m-%d')>=str_to_date("1/01/2015", '%m/%d/%Y')
and date_format(dateout, '%Y-%m-%d')<=str_to_date("12/31/2015", '%m/%d/%Y')
GROUP BY t2.empid,t2.catid
ORDER BY t2.empid,t2.catid;
可能希望包含一些示例数据。您正在使用'date_format(dateIn,'%Y-%m-%d')> = str_to_date(“2015年1月1日”,'%m /%d /%Y')'将一个字符串与日期进行比较。你应该总是比较类型。 – JRD