有2个表,一个叫user,一个叫group,是多对多的关系,也就是说一个用户可能属与多个组。另有一个表user_group记录对应关系,结构如下:
User
id name
5 小明
6 小强group:
id name
21 普通组
22 高级组
23 超级组user_group:
id userId groupId
1 5 22
2 5 23
3 6 21我想用写一个视图,得到这样的结果:
小明 高级组,超级组
小强 普通组请问应该如何实现呢?谢谢。
User
id name
5 小明
6 小强group:
id name
21 普通组
22 高级组
23 超级组user_group:
id userId groupId
1 5 22
2 5 23
3 6 21我想用写一个视图,得到这样的结果:
小明 高级组,超级组
小强 普通组请问应该如何实现呢?谢谢。
解决方案 »
- 查询记录合并问题,和早上一样在线等 解决就上分
- 我晕,这个T_SQL函数怎么写?
- 超难,这样的SQL语句怎么写?
- 重分求Sqlserver 2000英文版 ?在線等﹐立即給分!!!!!!!!!!!!!!!!!!!!
- A表的某个字段存有B表的多个ID,以逗号分隔。如何找出B表中哪些ID不存在于A表?
- 怎么实现sqlserver中:select * from A a where a.oid in(str[]);
- sql语句不执行
- 不好意思,再想问一下,如何可以知道网上有哪些SQLServer服务器可用
- 关于取得SQL SERVER数据库下表的字段名的提问
- 老大,在oracle 如何实现中文按拼音排序?
- 表查询
- 请教日期转化问题
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间: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')
gocreate function dbo.f_str(@id int) returns varchar(100)
as
begin
declare @str varchar(1000)
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 id , value = dbo.f_str(id) from tb group by iddrop function dbo.f_str
drop table tb
--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')
goselect id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by iddrop table tb
--3、使用游标合并数据
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
declare @t table(id int,value varchar(100))--定义结果集表变量
--定义游标并进行合并处理
declare my_cursor cursor local for
select id , value from tb
declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
open my_cursor
fetch my_cursor into @id , @value
select @id_old = @id , @s=''
while @@FETCH_STATUS = 0
begin
if @id = @id_old
select @s = @s + ',' + cast(@value as varchar)
else
begin
insert @t values(@id_old , stuff(@s,1,1,''))
select @s = ',' + cast(@value as varchar) , @id_old = @id
end
fetch my_cursor into @id , @value
END
insert @t values(@id_old , stuff(@s,1,1,''))
close my_cursor
deallocate my_cursorselect * from @t
drop table tb
create table t_user
(id int,
username varchar(4)
)insert into t_user
select 5,'小明' union all
select 6,'小强'create table t_group
(id int,
groupname varchar(6)
)
insert into t_group
select 21,'普通组' union all
select 22, '高级组' union all
select 23, '超级组'create table user_group
(id int,
userId int,
groupId int
)
insert into user_group
select 1,5,22 union all
select 2,5,23 union all
select 3,6,21create view view_answer
(username,groupname)
as
select a.username,b.groupname
from t_user a,t_group b,user_group c
where a.id=c.userId and b.id=c.groupIdselect *from view_answer 返回结果:
小明 高级组
小明 超级组
小强 普通组}
create table t_user
(id int,
username varchar(4)
)insert into t_user
select 5,'小明' union all
select 6,'小强'create table t_group
(id int,
groupname varchar(6)
)
insert into t_group
select 21,'普通组' union all
select 22, '高级组' union all
select 23, '超级组'create table user_group
(id int,
userId int,
groupId int
)
insert into user_group
select 1,5,22 union all
select 2,5,23 union all
select 3,6,21create view view_answer
(username,groupname)
as
select a.username,b.groupname
from t_user a,t_group b,user_group c
where a.id=c.userId and b.id=c.groupIdselect *from view_answer 返回结果:
小明 高级组
小明 超级组
小强 普通组
create table [user] ( id int, name varchar(10))
insert into [user]
select 5 ,'小明' union all
select 6 ,'小强'create table [group] (id int, name varchar(10))
insert into [group]
select 21, '普通组' union all
select 22, '高级组' union all
select 23, '超级组'3 6 21
create table user_group (id int, userid int,groupid int)
insert into user_group
select 1 ,5, 22 union all
select 2 ,5, 23 union all
select 3 ,6, 21;with tb
as(
select ug.id,u.name user_name,g.name group_name
from user_group ug join [user] u on u.id=ug.userid
left join [group] g on ug.groupid=g.id)
select user_name ,stuff((select ','+group_name
from tb
where user_name=t.user_name for xml path('')),1,1,'') group_name
from tb t
group by user_nameusername group_name
小明 高级组,超级组
小强 普通组