使 SQL 查询动态化
Making SQL query dynamic
我有以下 SQL 查询,我想知道我是否可以使它的 SUM() 部分动态化,这样我就不必输入 category_id(2 和 3)
SELECT
a.project_id,
COUNT(a.id) AS Total,
SUM(CASE WHEN a.category_id = 2 AND a.`status` < 80 THEN 1 ELSE 0 END) AS 'Bugs En cours',
SUM(CASE WHEN a.category_id = 2 AND a.`status` >= 80 THEN 1 ELSE 0 END) AS 'Bugs Resolu',
SUM(CASE WHEN a.category_id = 3 AND a.`status` < 80 THEN 1 ELSE 0 END) AS 'Ameliorations En cours',
SUM(CASE WHEN a.category_id = 3 AND a.`status` >= 80 THEN 1 ELSE 0 END) AS 'Ameliorations Resolu'
FROM bugs a
GROUP BY a.project_id
HAVING COUNT(a.id) > 0
目标是根据 category_id
和 status
('En cours' 或 'Resolu')
此查询中的问题是,如果我们添加另一个类别,我将不得不手动编辑此查询,这并不理想。
下面的动态 sql 正在根据具有类别的参考 table 构建条件总和。
SET @sums := (
SELECT GROUP_CONCAT(
CONCAT(
', SUM(CASE WHEN category_id = ', category_id,
' AND `status` < 80 THEN 1 ELSE 0 END)',
' AS `', category_name, ' En cours`', CHAR(10),
', SUM(CASE WHEN category_id = ', category_id,
' AND `status` >= 80 THEN 1 ELSE 0 END)',
' AS `', category_name, ' Resolu`', CHAR(10)
) ORDER BY category_id SEPARATOR '') as sums
FROM categories
);
SET @dynsql := CONCAT('SELECT', CHAR(10), ' bug.project_id', CHAR(10),
', COUNT(bug.id) AS Total', CHAR(10),
@sums,
'FROM bugs bug', CHAR(10),
'GROUP BY bug.project_id', CHAR(10),
'HAVING COUNT(bug.id) > 0;');
-- SELECT @dynsql AS dynsql;
PREPARE dyn_stmt FROM @dynsql;
EXECUTE dyn_stmt;
DEALLOCATE PREPARE dyn_stmt;
project_id
Total
Conneries En cours
Conneries Resolu
Bugs En cours
Bugs Resolu
Ameliorations En cours
Ameliorations Resolu
0
5
1
0
1
1
1
1
演示 db<>fiddle here
我有以下 SQL 查询,我想知道我是否可以使它的 SUM() 部分动态化,这样我就不必输入 category_id(2 和 3)
SELECT
a.project_id,
COUNT(a.id) AS Total,
SUM(CASE WHEN a.category_id = 2 AND a.`status` < 80 THEN 1 ELSE 0 END) AS 'Bugs En cours',
SUM(CASE WHEN a.category_id = 2 AND a.`status` >= 80 THEN 1 ELSE 0 END) AS 'Bugs Resolu',
SUM(CASE WHEN a.category_id = 3 AND a.`status` < 80 THEN 1 ELSE 0 END) AS 'Ameliorations En cours',
SUM(CASE WHEN a.category_id = 3 AND a.`status` >= 80 THEN 1 ELSE 0 END) AS 'Ameliorations Resolu'
FROM bugs a
GROUP BY a.project_id
HAVING COUNT(a.id) > 0
目标是根据 category_id
和 status
('En cours' 或 'Resolu')
此查询中的问题是,如果我们添加另一个类别,我将不得不手动编辑此查询,这并不理想。
下面的动态 sql 正在根据具有类别的参考 table 构建条件总和。
SET @sums := ( SELECT GROUP_CONCAT( CONCAT( ', SUM(CASE WHEN category_id = ', category_id, ' AND `status` < 80 THEN 1 ELSE 0 END)', ' AS `', category_name, ' En cours`', CHAR(10), ', SUM(CASE WHEN category_id = ', category_id, ' AND `status` >= 80 THEN 1 ELSE 0 END)', ' AS `', category_name, ' Resolu`', CHAR(10) ) ORDER BY category_id SEPARATOR '') as sums FROM categories ); SET @dynsql := CONCAT('SELECT', CHAR(10), ' bug.project_id', CHAR(10), ', COUNT(bug.id) AS Total', CHAR(10), @sums, 'FROM bugs bug', CHAR(10), 'GROUP BY bug.project_id', CHAR(10), 'HAVING COUNT(bug.id) > 0;'); -- SELECT @dynsql AS dynsql; PREPARE dyn_stmt FROM @dynsql; EXECUTE dyn_stmt; DEALLOCATE PREPARE dyn_stmt;
project_id | Total | Conneries En cours | Conneries Resolu | Bugs En cours | Bugs Resolu | Ameliorations En cours | Ameliorations Resolu |
---|---|---|---|---|---|---|---|
0 | 5 | 1 | 0 | 1 | 1 | 1 | 1 |
演示 db<>fiddle here