日期:2014-05-17 浏览次数:20623 次
create table A(ID int,UserName nvarchar(40),BasicPower nvarchar(100))
insert into A
select 1,'admin',',1,2,3,' union all
select 2,'Tom',',2,3,' union all
select 3,'Jack',',3,'
select * from A
create table B(ID int,ClassName nvarchar(50))
insert into B
select 1,'系统设置' union all
select 2,'留言管理' union all
select 3,'评论管理'
select* from B
select id,UserName,
BasicPower=stuff((select ','+ ClassName from B
where charindex(rtrim(id)+',',A.BasicPower+',')>0 for xml path('')),1,1,'')
from A group by id,UserName,BasicPower
drop table A;
drop table B;
select a.id,a.username,
GROUP_CONCAT(DISTINCT classname ORDER BY b.id asc SEPARATOR ",") as basicpower
from a,b where find_in_set(b.ID,a.basicpower)
group by a.id;