嗨,我有一个动态表,看起来像这样,
CREATE TABLE Table1
(`ID_Question` varchar(2), `ID_Solution` varchar(4), `ID_Mapping` int)
;
INSERT INTO Table1
(`ID_Question`, `ID_Solution`, `ID_Mapping`)
VALUES
('B1', 'GFC1', 1),
('B1', 'GFC2', 0),
('B1', 'GFC3', 0),
('B1', 'AFC1', 1),
('B1', 'AFC2', 0),
('B2', 'GFC1', 0),
('B2', 'GFC2', 1),
('B2', 'GFC3', 0),
('B2', 'AFC1', 0),
('B2', 'AFC2', 1),
('B2', 'GFC1', 0),
('B2', 'GFC2', 1),
('B2', 'GFC3', 0),
('B2', 'AFC1', 0),
('B2', 'AFC2', 1)
;
基本上看起来像这样,
+-------------+-------------+------------+
| ID_Question | ID_Solution | ID_Mapping |
+-------------+-------------+------------+
| B1 | GFC1 | 1 |
| B1 | GFC2 | 0 |
| B1 | GFC3 | 0 |
| B1 | AFC1 | 1 |
| B1 | AFC2 | 0 |
| B2 | GFC1 | 0 |
| B2 | GFC2 | 1 |
| B2 | GFC3 | 0 |
| B2 | AFC1 | 0 |
| B2 | AFC2 | 1 |
| B2 | GFC1 | 0 |
| B2 | GFC2 | 1 |
| B2 | GFC3 | 0 |
| B2 | AFC1 | 0 |
| B2 | AFC2 | 1 |
+-------------+-------------+------------+
我想知道我是否可以动态显示(可以在其中添加和删除数据),例如:
+-----------+------+------+------+------+------+
| ID_SOLUSI | GFC1 | GFC2 | GFC3 | ASF1 | AFC2 |
+-----------+------+------+------+------+------+
| B1 | 1 | 0 | 0 | 1 | 1 |
+-----------+------+------+------+------+------+
| B2 | 0 | 1 | 0 | 1 | 0 |
+-----------+------+------+------+------+------+
| B3 | 1 | 0 | 0 | 0 | 1 |
+-----------+------+------+------+------+------+
我尝试使用它,但似乎不起作用:
SET @sql_ = (
SELECT
GROUP_CONCAT(DISTINCT(CONCAT("ID_mapping AS " , ID_Solution))
FROM table1
);
SET @SQL = CONCAT('SELECT ID_QUESTION, ',
@sql_dinamis , '
FROM tabl1
GROUP BY ID_QUESTION
WITH ROLLUP'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
如果您还可以向我解释这个概念,那将是非常不错的,因为我有点讨厌SQL查询(非常棒)
动态SQL背后的想法(您试图在这里使用的想法是,使用查询生成另一个查询,然后可以执行该查询。)>
这可能有助于首先可视化您尝试动态构建的查询。对于您的样本数据,应为:
select id_question, max(case when id_solution = 'AFC1' then id_mapping end) as `AFC1`, max(case when id_solution = 'AFC2' then id_mapping end) as `AFC2`, max(case when id_solution = 'GFC1' then id_mapping end) as `GFC1`, max(case when id_solution = 'GFC2' then id_mapping end) as `GFC2`, max(case when id_solution = 'GFC3' then id_mapping end) as `GFC3` from Table1 group by id_question with rollup
这里是用于此目的的代码:
-- generate the `max()` expressions in the `from` clause
set @sql = (
select group_concat(
distinct 'max(case when id_solution = ''', id_solution, ''' then id_mapping end) as `', id_solution, '`'
order by id_solution
separator ', '
) from Table1
);
-- debug
select @sql;
-- now build the rest of the query string
set @sql = concat('select id_question, ', @sql, ' from Table1 group by id_question with rollup');
-- debug
select @sql;
-- finally, run the query for good
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;