2个回答
展开全部
sqlserver下,表数据
create table a
(id int,
name varchar(10),
note varchar(10))
insert into a values (1,'A','AAA')
insert into a values (2,'B','BBB')
insert into a values (3,'C','CCC')
insert into a values (4,'D','DDD')
create table b
(id int,
name varchar(10),
[key] int,
var varchar(10))
insert into b values (1,'A',1,'AA')
insert into b values (1,'A',2,'BB')
insert into b values (2,'B',1,'CC')
insert into b values (2,'B',2,'DD')
insert into b values (3,'C',1,'EE')
insert into b values (3,'C',2,'FF')
insert into b values (4,'D',1,'GG')
insert into b values (4,'D',2,'HH')
执行:
select a.id,a.name,max(case when b.[key]=1 then b.var end) key1,max(case when b.[key]=2 then b.var end) key2,a.note
from a,b where a.id=b.id
group by a.id,a.name,a.note
结果:
其他数据库语法基本一致
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询