日期:2014-05-17  浏览次数:20542 次

如何将A表的多个查询结果存入B表的某一个列中?
有两张表:
A表:
-----------------
name pos class  
g1 11 chr1
g2 21 chr1
g3 05 chr2
g4 15 chr1
-------------------

B表:
------------------
name pos class  
g21 1 chr1  
g22 20 chr1
g23 6 chr2
g24 20 chr1
------------------

要求是:A.class=B.class and B.pos<A.pos 并将查询出来的A.name存到 新B.insert中。 查询后的结果新B表为:
------------------
name pos class insert 
g21 1 chr1 g1,g2,g4  
g22 2 chr1 g2
g23 6 chr2  
g24 20 chr1 g2
------------------
求实现的SQL语句,谢谢!

------解决方案--------------------
SQL code
select *,
       [insert]=stuff((select ','+name from A表 
               where class=a.class and b.pos<pos for xml path('')),1,1,'')
from B表 b

------解决方案--------------------
SQL code

create table a1(name varchar(20), pos  varchar(20), class  varchar(20))
insert into a1 values('g1','11','chr1')
insert into a1 values('g2','21','chr1')
insert into a1 values('g3','05','chr2')
insert into a1 values('g4','15','chr1')   
-------------------
create table b1(name varchar(20), pos  varchar(20), class  varchar(20),[insert] varchar(20))
insert into b1 values('g21','1','chr1',null)
insert into b1 values('g22','20','chr1',null)
insert into b1 values('g23','6','chr2',null)
insert into b1 values('g24','20','chr1',null) 

select * from b1
update b1 set
[insert]=e.myvalue
from (select B.name, myvalue=stuff((
select ','+aname from(select A0.name as aname,B0.name as bname from a1 as A0,b1 as B0  where A0.class=B0.class and B0.pos<A0.pos)d
where bname=B.name for xml path('')
 ), 1 , 1 , '') from b1 as B  
 group by B.name)e
 where e.name=b1.name
 
 select * from b1
 
 drop table a1,b1


(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)
name                 pos                  class                insert
-------------------- -------------------- -------------------- --------------------
g21                  1                    chr1                 NULL
g22                  20                   chr1                 NULL
g23                  6                    chr2                 NULL
g24                  20                   chr1                 NULL

(4 行受影响)

(4 行受影响)

name                 pos                  class                insert
-------------------- -------------------- -------------------- --------------------
g21                  1                    chr1                 g1,g4,g2
g22                  20                   chr1                 g2
g23                  6                    chr2                 NULL
g24                  20                   chr1                 g2

(4 行受影响)

------解决方案--------------------
SQL code
/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳

描述:将如下形式的数据按id字段合并value字段。
id    value
----- ------
1     aa
1     bb
2     aaa
2     bbb
2     ccc
需要得到结果:
id     value
------ -----------
1      aa,bb
2      aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
*/
--1、sql2000中只能用自定义的函数解决
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

create function dbo.f_str(@id varchar(10)) returns varchar(1000)
as
begin
  declare @str varchar(1000)
  select @str = isnull(@str + ',' , '') + cast(value as varchar) from tb where id = @id
  return @str
end
go

--调用函数
select id , value = dbo.f_str(id) from tb group by id

drop function dbo.f_str
drop table tb


--2、sql2005中的方法
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert