gpt4 book ai didi

mysql - 显示结果 - 根据条件将 1 列中的数据分为 2 列

转载 作者:行者123 更新时间:2023-11-29 13:34:15 26 4
gpt4 key购买 nike

您好,我需要帮助...

我的查询显示以下结果:

Id      name           color    Version
1 leather black 1
1 leather brown 2
2 suede brown 1
3 cloth green 1
3 cloth blue 2

我想显示以下内容:

Id      name           color    Color_2    
1 leather black brown
2 suede brown
3 cloth green blue

查询很简单目前

SELECT ID, NAME, COLOR,VERSION
FROM table1,table2
WHERE table1.ID = table2.ID
AND id in
(SELECT ID
FROM table1,table2
WHERE table1.ID = table2.ID
AND VERSION in ('1'))
AND VERSION in ('1','2')

最佳答案

一个简单的(如果您在设计时知道您可能拥有的最大颜色数)

drop table my_test;

create table my_test (
id number,
name varchar2(32),
color varchar2(32),
version number);

insert into my_test values(1,'leather','black',1);
insert into my_test values(1,'leather','brown',2);
insert into my_test values(2,'suede','brown',1);
insert into my_test values(3,'cloth','green',1);
insert into my_test values(3,'cloth','blue ',2);

set linesize 200
select min(id) id,
name,
max(decode(version,1,color,null)) color,
max(decode(version,2,color,null)) color_2
from my_test
group by name
order by 1;

ID NAME COLOR COLOR_2
---------- ---------- ---------- ----------
1 leather black brown
2 suede brown
3 cloth green blue

3 rows selected.

这适用于任何 Oracle 数据库版本。根据您使用的版本,查看 LISTAGG、WM_CONCAT 等( here )

关于mysql - 显示结果 - 根据条件将 1 列中的数据分为 2 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18964223/

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