-1

计数函数没有错误地显示任何记录。

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 ;
4

1 回答 1

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

它产生不正确值的主要原因之一是由于未指定两个表的关系,连接表会产生笛卡尔积。这是一个没有关系定义的内部连接的示例单击此链接进行演示

于 2013-01-06T13:03:52.373 回答