值得注意的一个SQL帖子[]
2005-03-11 09:37
260 查看
表内容如下a,U_ID相同的将 R_MOdule字段中相同部分取出来,并逗号割开(这里就是第一第二行),
如果以逗号格开的字符串相同就提取出来,也就是要得到b表的意思。
a
U_ID R_MOdule
----------- --------------------------------------------------
4 8,9,10,11,12,15,16,17,18,19,20,21,22,23,24,25,28
4 1,8,14,18
8 1,8,14,18
b
U_ID R_Module
4 8,18
8 1,8,14,18
答案内详细!
create table tb(U_ID int,R_MOdule varchar(8000))
insert tb select 4,'8,9,10,11,12,15,16,17,18,19,20,21,22,23,24,25,28'
union all select 4,'1,8,14,18'
union all select 4,'1,8,14,18,17'
union all select 8,'1,8,14,18'
go
--合并数据增加的辅助表
select top 8000 id=identity(int) into 序数表 from syscolumns a,syscolumns b
go
--合并处理函数
create function f_merg(@U_ID int)
returns varchar(8000)
as
begin
declare @s nvarchar(4000)
set @s=''
select @s=@s+','+s
from(
select top 100 percent
s=substring(a.R_MOdule,b.id,charindex(',',a.R_MOdule+',',b.id)-b.id)
from tb a,序数表 b
where a.U_ID=@U_ID and b.id<=len(a.R_MOdule) and substring
(','+a.R_MOdule,b.id,1)=','
group by substring(a.R_MOdule,b.id,charindex(',',a.R_MOdule+',',b.id)-b.id)
having count(*)=(select count(*) from tb where U_ID=@U_ID)
order by min(id))a
return(stuff(@s,1,1,''))
end
GO
--调用函数进行处理
select U_ID,R_MOdule=dbo.f_merg(U_ID) from tb group by U_ID
go
--删除测试
drop table tb,序数表
drop function f_merg
--运行结果:
U_ID R_Module
4 8,18
8 1,8,14,18
原帖地址:http://community.csdn.net/Expert/topic/3841/3841961.xml?temp=.6100428
如果以逗号格开的字符串相同就提取出来,也就是要得到b表的意思。
a
U_ID R_MOdule
----------- --------------------------------------------------
4 8,9,10,11,12,15,16,17,18,19,20,21,22,23,24,25,28
4 1,8,14,18
8 1,8,14,18
b
U_ID R_Module
4 8,18
8 1,8,14,18
答案内详细!
create table tb(U_ID int,R_MOdule varchar(8000))
insert tb select 4,'8,9,10,11,12,15,16,17,18,19,20,21,22,23,24,25,28'
union all select 4,'1,8,14,18'
union all select 4,'1,8,14,18,17'
union all select 8,'1,8,14,18'
go
--合并数据增加的辅助表
select top 8000 id=identity(int) into 序数表 from syscolumns a,syscolumns b
go
--合并处理函数
create function f_merg(@U_ID int)
returns varchar(8000)
as
begin
declare @s nvarchar(4000)
set @s=''
select @s=@s+','+s
from(
select top 100 percent
s=substring(a.R_MOdule,b.id,charindex(',',a.R_MOdule+',',b.id)-b.id)
from tb a,序数表 b
where a.U_ID=@U_ID and b.id<=len(a.R_MOdule) and substring
(','+a.R_MOdule,b.id,1)=','
group by substring(a.R_MOdule,b.id,charindex(',',a.R_MOdule+',',b.id)-b.id)
having count(*)=(select count(*) from tb where U_ID=@U_ID)
order by min(id))a
return(stuff(@s,1,1,''))
end
GO
--调用函数进行处理
select U_ID,R_MOdule=dbo.f_merg(U_ID) from tb group by U_ID
go
--删除测试
drop table tb,序数表
drop function f_merg
--运行结果:
U_ID R_Module
4 8,18
8 1,8,14,18
原帖地址:http://community.csdn.net/Expert/topic/3841/3841961.xml?temp=.6100428
相关文章推荐
- 值得注意的一个SQL帖子[]
- 一个宏定义值得注意的地方
- mybatis if 动态生成SQL的一个注意点
- Hibernate查询,返回new对象(注意这个新定义的类要有构造函数),使用sql带条件分页查询并且把结果显示到一个对象的集里面的解决方案
- ajax post中在firefox返回值一个值得注意的地方
- 【问题记录】python 函数 传入一个对象返回一个对象值得注意
- Hibernate查询,返回new对象(注意这个新定义的类要有构造函数),使用sql带条件分页查询并且把结果显示到一个对象的集里面的解决方案
- 一个值得纪念的SQL查询语句
- Qt国际化——多语言翻译时值得注意的一个编程问题
- SQL中的两个值得注意的特殊符号
- UserControl 的一个值得注意的问题 [属性" * "的代码生成失败.错误是:"程序集"*.Version=1.0.0.0,Culture=neutral,..........无标记为序列化"
- 一个SQL语句要注意的问题
- Android 动画效果 --Animation 动画(讲解了所有的Android动画效果,是一个值得收藏的帖子)
- mybatis if 动态生成SQL的一个注意点 parameterType="map"
- csdn里面的一个帖子关于sql
- Android 动画效果 --Animation 动画(讲解了所有的Android动画效果,是一个值得收藏的帖子)
- 在DLL中创建窗口时一个值得注意的地方 —— UnregisterClass
- 一个24小时内点击最高的新闻sql语句 + require()注意事项
- ajax post中在firefox返回值一个值得注意的地方
- Android 动画效果 --Animation 动画(讲解了所有的Android动画效果,是一个值得收藏的帖子)