Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Select-Anweisung innerhalb einer Schleife in einer gespeicherten Mysql-Prozedur

Versuchen Sie Folgendes:

DELIMITER $$

DROP PROCEDURE IF EXISTS `AbsentReportproc`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `AbsentReportproc`(IN _fromdate DATETIME, IN _todate DATETIME)
BEGIN 
    CREATE TEMPORARY TABLE daterange (dte DATE); 

    SET @counter := -1;
    WHILE (@counter < DATEDIFF(DATE(_todate), DATE(_fromdate))) DO 
        INSERT daterange VALUES (DATE_ADD(_fromdate, INTERVAL @counter:[email protected] + 1 DAY));
    END WHILE;

    SELECT tp.cardno, tp.EMPCODE, tp.DEPARTMENT, GROUP_CONCAT(d.dte) Absentddate, COUNT(tp.cardno) Totalnoofabsentdates
    FROM test_prefixmaster tp JOIN daterange d 
    LEFT JOIN test_prefixtransactions tpt ON tp.cardno = tpt.CARDNO AND DATE(S_DateTime) = d.dte
    WHERE tpt.CARDNO IS NULL 
    GROUP BY tp.cardno;

    DROP TABLE daterange;
END$$

DELIMITER ;