任何人都可以帮助我。
SET @dynQuery = CONCAT("CREATE TEMPORARY TABLE temp_info
SELECT
exitStat.department_id AS department_id
,exitStat.cadre_type_id AS cadre_id
,dept.name AS department_name
,lkup.lkup_name
,'TEMP' AS row_type
,0 AS lvl
,0 AS children");
WHILE(fromMon <= toMon) DO
SET dateColName = REPLACE(fromMon, "-", "_");
SET @dynQuery = CONCAT(@dynQuery,
", exitStat.", dateColName , "_", joiners,
", exitStat.", dateColName , "_", exits
);
SET fromMon = DATE_ADD(fromMon, INTERVAL 1 MONTH);
END WHILE;
SET @dynQuery = CONCAT(@dynQuery, " FROM exit_statistics exitStat
JOIN lkup_types lkup ON lkup.id = exitStat.cadre_type_id
JOIN departments dept ON dept.id = exitStat.department_id");
IF departmentId > 0 AND cadreTypeId > 0 THEN
SET @dynQuery = CONCAT(@dynQuery, " WHERE exitStat.department_id IN (", departmentId ,") AND exitStat.cadre_type_id IN (", cadreTypeId, ")");
ELSEIF departmentId > 0 THEN
SET @dynQuery = CONCAT(@dynQuery, " WHERE exitStat.department_id IN (", departmentId, ")");
ELSEIF cadreTypeId > 0 THEN
SET @dynQuery = CONCAT(@dynQuery, " WHERE exitStat.cadre_type_id IN (", cadreTypeId, ")");
END IF;
PREPARE stmt FROM @dynQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;