gpt4 book ai didi

mysql - 在mysql中选择动态列

转载 作者:可可西里 更新时间:2023-11-01 07:16:51 26 4
gpt4 key购买 nike

是否可以像这样遍历一个表:

mysql> select * from `stackoverflow`.`Results`;+--------------+---------+-------------+--------+| ID           | TYPE    | CRITERIA_ID | RESULT |+--------------+---------+-------------+--------+|            1 | car     | env         | 1      ||            2 | car     | gas         |        ||            3 | car     | age         |        ||            4 | bike    | env         | 1      ||            5 | bike    | gas         |        ||            6 | bike    | age         | 1      ||            7 | bus     | env         | 1      ||            8 | bus     | gas         | 1      ||            9 | bus     | age         | 1      |+--------------+---------+-------------+--------+9 rows in set (0.00 sec)

Into this:

+------+-----+-----+-----+| TYPE | env | gas | age |+------+-----+-----+-----+| car  | 1   |     |     || bike | 1   |     | 1   || bus  | 1   | 1   | 1   |+------+-----+-----+-----+

The aim is to select all the CRITERIA_IDs and use them as a column.As rows i like to use all the TYPEs .

  • All Criterias: SELECT distinct(CRITERIA_ID) FROM stackoverflow.Results;
  • All Types SELECT distinct(TYPE) FROM stackoverflow.Results;

But how combine them into a view or smth. like this?

If you like to play with the data. This is a script to generate the table:

CREATE SCHEMA `stackoverflow`;
CREATE TABLE `stackoverflow`.`Results` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`TYPE` varchar(50) NOT NULL,
`CRITERIA_ID` varchar(5) NOT NULL,
`RESULT` bit(1) NOT NULL,
PRIMARY KEY (`ID`)
)
ENGINE=InnoDB;

INSERT INTO `stackoverflow`.`Results`
(
`ID`,
`TYPE`,
`CRITERIA_ID`,
`RESULT`
)
VALUES
( 1, "car", env, true ),
( 2, "car", gas, false ),
( 3, "car", age, false ),
( 4, "bike", env, true ),
( 5, "bike", gas, false ),
( 6, "bike", age, true ),
( 7, "bus", env, true ),
( 8, "bus", gas, true ),
( 9, "bus", age, true );

最佳答案

不幸的是,MySQL 没有 PIVOT 函数,这基本上就是您要尝试做的。因此,您需要使用带有 CASE 语句的聚合函数:

SELECT type,
sum(case when criteria_id = 'env' then result end) env,
sum(case when criteria_id = 'gas' then result end) gas,
sum(case when criteria_id = 'age' then result end) age
FROM results
group by type

参见 SQL Fiddle with Demo

现在,如果您想动态地执行此操作,这意味着您事先不知道要转置的列,那么您应该查看以下文章:

Dynamic pivot tables (transform rows to columns)

您的代码如下所示:

SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(IF(CRITERIA_ID = ''',
CRITERIA_ID,
''', RESULT, NULL)) AS ',
CRITERIA_ID
)
) INTO @sql
FROM
Results;
SET @sql = CONCAT('SELECT type, ', @sql, ' FROM Results GROUP BY type');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

参见 SQL Fiddle with Demo

关于mysql - 在mysql中选择动态列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11899647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com