计数功能不会错误地显示任何记录。
DELIMITER $$
DROP PROCEDURE IF EXISTS `AbsentReportproctest`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `AbsentReportproctest`(IN _fromdate DATETIME, IN _todate DATETIME)
BEGIN
CREATE TEMPORARY TABLE daterange25 (dte DATE);
CREATE TEMPORARY TABLE daterange26 (dte DATE);
SET @counter := -1;
WHILE (@counter < DATEDIFF(DATE(_todate), DATE(_fromdate))) DO
INSERT INTO daterange25 VALUES (DATE_ADD(_fromdate, INTERVAL @counter:=@counter + 1 DAY));
INSERT INTO daterange26 VALUES (DATE_ADD(_fromdate, INTERVAL @counter:=@counter + 1 DAY));
END WHILE;
SELECT tp.EMPCODE, d.dte,gc.count
FROM test_prefixmaster tp
INNER JOIN(SELECT tp.EMPCODE,COUNT(*)AS count
FROM test_prefixmaster tp
JOIN daterange26 d
LEFT JOIN test_prefixtransactions tpt ON (tp.EMPCODE = tpt.empcode)
AND DATE(S_DateTime) = d.dte
WHERE tpt.empcode IS NULL
GROUP BY tp.EMPCODE) gc on tp.EMPCODE = gc.EMPCODE
JOIN daterange25 d
LEFT JOIN test_prefixtransactions tpt ON (tp.EMPCODE = tpt.empcode)
AND DATE(S_DateTime) = d.dte
WHERE tpt.empcode IS NULL;
DROP TABLE daterange25;
DROP TABLE daterange26;
END$$
DELIMITER ;
答案 0 :(得分:2)
我不知道这是否是你问题中的拼写错误,但在你的加入中,我发现你没有在你的表之间提供ON
条款,
SELECT tp.EMPCODE,
d.dte,
gc.count
FROM test_prefixmaster tp
INNER JOIN
(
SELECT tp.EMPCODE,
COUNT(*) AS count
FROM test_prefixmaster tp
INNER JOIN daterange26 d
-- where is the ON clause here ??
LEFT JOIN test_prefixtransactions tpt
ON (tp.EMPCODE = tpt.empcode)
AND DATE (S_DateTime) = d.dte
WHERE tpt.empcode IS NULL
GROUP BY tp.EMPCODE
) gc
ON tp.EMPCODE = gc.EMPCODE
INNER JOIN daterange25 d
-- where is the ON clause here ??
LEFT JOIN test_prefixtransactions tpt
ON (tp.EMPCODE = tpt.empcode)
AND DATE (S_DateTime) = d.dte
WHERE tpt.empcode IS NULL
产生错误值的主要原因之一是,因为连接表由于未指定两个表的关系而产生笛卡尔积。这是一个没有关系的内部联接定义Click this link for demo
的例子