求助

孤舟风雨 2012-01-16 10:36:41
表一:
id name
1 aa
2 bb
表二:
Tid Tname
1 测试
1 评论
1 测试1
2 DD
2 DC
2 BD
查询出来的结果是:
id name Tname
1 aa 测试,评论,测试1
2 bb DD,DC,BD
...全文
49 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
勿勿 2012-01-16
  • 打赏
  • 举报
回复
--表一:
--id name
--1 aa
--2 bb
--表二:
--Tid Tname
--1 测试
--1 评论
--1 测试1
--2 DD
--2 DC
--2 BD


if OBJECT_ID('aa') is not null
drop table aa
go
create table aa (id int,name varchar(50))
insert into aa values(1,'aa')
insert into aa values(2,'bb')

if OBJECT_ID('bb') is not null
drop table bb
go
create table bb (Tid int,Tname varchar(50))
insert into bb values(1,'测试')
insert into bb values(1,'评论')
insert into bb values(1,'测试1')
insert into bb values(2,'DD')
insert into bb values(2,'DD')
insert into bb values(2,'BD')
--查询出来的结果是:
--id name Tname
--1 aa 测试,评论,测试1
--2 bb DD,DC,BD


select a.id,a.name ,
stuff((select ',' + Tname from bb where Tid=a.id for xml path ('')), 1,1,'') as 测试1 from aa a
group by a.id,a.name



id name 测试1
----------- -------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 aa 测试,评论,测试1
2 bb DD,DD,BD

(2 行受影响)


唐诗三百首 2012-01-16
  • 打赏
  • 举报
回复

create table tab1
(id int, name char(5))

insert into tab1
select 1, 'aa' union all
select 2, 'bb'

create table tab2
(Tid int, Tname varchar(6))

insert into tab2
select 1, '测试' union all
select 1, '评论' union all
select 1, '测试1' union all
select 2, 'DD' union all
select 2, 'DC' union all
select 2, 'BD'


select a.id,a.name,
left(b.Tname2,len(b.Tname2)-1) 'Tname'
from tab1 a
inner join (
select t2.Tid,
cast((select Tname+',' from tab2 t3 where t3.Tid=t2.Tid for xml path(''))
as varchar) Tname2
from tab2 t2 group by t2.Tid) b
on a.id=b.Tid

id name Tname
----------- ----- ------------------------------
1 aa 测试,评论,测试1
2 bb DD,DC,BD

(2 row(s) affected)
叶子 2012-01-16
  • 打赏
  • 举报
回复

--合并列值
--原著:邹建
--改编:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-12-16 广东深圳

--表结构,数据如下:
/*
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
*/
--需要得到结果:
/*
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求value 的和(字符串相加)
*/
--1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--1. 创建处理函数
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 int)
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r = ''
select @r = @r + ',' + value from tb where id=@id
return stuff(@r, 1, 1, '')
end
go

-- 调用函数
SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id

drop table tb
drop function dbo.f_str

/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为2 行)
*/

--2、另外一种函数.
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 f_hb(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as value from tb

drop table tb
drop function dbo.f_hb

/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为2 行)
*/

--2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
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
-- 查询处理
select * from(select distinct id from tb)a outer apply(
select [values]= stuff(replace(replace(
(
select value from tb n
where id = a.id
for xml auto
), ' <N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb

/*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc

(2 行受影响)
*/

--SQL2005中的方法
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

select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id

/*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc

(2 row(s) affected)

*/

drop table tb

百年树人 2012-01-16
  • 打赏
  • 举报
回复
if object_id('[t1]') is not null drop table [t1]
go
create table [t1]([id] int,[name] varchar(2))
insert [t1]
select 1,'aa' union all
select 2,'bb'
if object_id('[t2]') is not null drop table [t2]
go
create table [t2]([Tid] int,[Tname] varchar(5))
insert [t2]
select 1,'测试' union all
select 1,'评论' union all
select 1,'测试1' union all
select 2,'DD' union all
select 2,'DC' union all
select 2,'BD'

select a.id,a.name,
Tname=stuff((select ','+tname from t2 where tid=a.id for xml path('')),1,1,'')
from t1 a
group by a.id,a.name

/**
id name Tname
----------- ---- ----------------------
1 aa 测试,评论,测试1
2 bb DD,DC,BD

(2 行受影响)
**/
AcHerat 2012-01-16
  • 打赏
  • 举报
回复

select *,stuff((select ','+tname from tb2 where id = t.id for xml path('')),1,1,'') as tname
from tb1 t
group by id,name

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧