测试表,数据:
create table a(name varchar(10),
mdid int)
insert into a values ('张三',1)
insert into a values ('张三',2)
insert into a values ('李四',1)
insert into a values ('王五',2)
insert into a values ('王五',3)
create table b
(mdid int,
mdname varchar(10),
money int)
insert into b values (1,'北京',2000)
insert into b values (2,'上海',2500)
insert into b values (3,'广州',1800)
执行:
select name,mdname=stuff((select '/'+mdname from
(select a.name,b.mdname,b.money from a,b where a.mdid=b.mdid) t where name=tb.name for xml path('')), 1, 1, ''),
money=stuff((select '/'+ cast(money as varchar) from
(select a.name,b.mdname,b.money from a,b where a.mdid=b.mdid) t where name=tb.name for xml path('')), 1, 1, '')
from (select a.name,b.mdname,b.money from a,b where a.mdid=b.mdid) tb
group by name
结果:
以上:sqlserver下执行。
注意:是插入还是更新?插入的话:
insert into a(col) select col from b
更新的话:
update a set col=select col from b where a.id=b.id
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)