表A
aid aname
----------
1 aa
2 bb
3 cc
表B
bid bname aid bpath
---------------------
1 ds 1 p1
2 fsd 1 p2
3 ds 2 p3
4 dgs 3 p4
5 fds 3 p5
想要的结果:
aid bpath
-------------
1 p1,p2
2 p3
3 p4,p5
各位大哥大姐 帮帮小弟吧~~~
------解决方案--------------------------------------------------------
Create function GetbPath(@aid int)returns varchar(1000)Begindeclare @str varchar(1000)set @str = ' 'select @str = @str + bpath+ ', ' from b where aid=@aidif @str!= ' ' set @str = left(@str,len(@str)-1)return @strend
------解决方案--------------------------------------------------------
然后:select a.aid,dbo.GetbPath(a.aid) from a
------解决方案--------------------------------------------------------
cpp2017(慕白兄) 方法不错
我刚准备说用游标的;呵呵
------解决方案--------------------------------------------------------
Create table tb (aid int,bpath varchar(2))
insert into tb select 1, 'p1 '
insert into tb select 1, 'p2 '
insert into tb select 2, 'p3 '
insert into tb select 3, 'p4 '
insert into tb select 3, 'p5 '
Create function aid(@aid int)
returns varchar(1000)
begin
declare @str varchar(1000)
set @str= ' '
select @str=@str+ ', '+ bpath from tb where aid=@aid
set @str=right(@str,len(@str)-1)
return(@str)
End
select aid,dbo.aid(aid) from tb a
group by aid
aid
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 p1,p2
2 p3
3 p4,p5
(所影响的行数为 3 行)
------解决方案--------------------------------------------------------
慢了一步
------解决方案--------------------------------------------------------
来晚了
------解决方案--------------------------------------------------------
不用存储过程可以实现吗??