SQL2005,有tb表
id name tIDs
1 a 1,2,3
2 b 1,2
TbIDs表
id IdName
1 我
2 你
3 他
-------------
想要结果
id name idnames
1 a 我,你,他
2 b 我,你
id name tIDs
1 a 1,2,3
2 b 1,2
TbIDs表
id IdName
1 我
2 你
3 他
-------------
想要结果
id name idnames
1 a 我,你,他
2 b 我,你
解决方案 »
- WCF的问题。。。有没有人遇到过?
- xslt 如何获取xml节点的问题
- brother help
- 急啊!(c#)怎样获取HTML里自己想要的数据呢?马上结帐!
- [求助]关于ListBox绑定前判断选定项的问题,很急,在线等
- 关于javascript对象
- treeview使用问题?(树型数据库用treeview显示)
- ★★难到了WEB开发和.NET的所有版主的问题★★仍然无法解决,SESSION超时SESSION超时
- 是否可以将asp.net应用程序转换成jsp的工具??
- 奇葩ie:asp.net的LinkButton与TreeView的节点点击没反应
- asp.net小菜鸟寻求帮助关于数据库更新图片功能,急!
- 请问如何实现数字页码条?
具体函数就不写了
分太少
--> 测试数据:[tb]
--> 我的淘宝:http://shop36766744.taobao.com/ if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[name] varchar(1),[tIDs] varchar(5))
insert [tb]
select 1,'a','1,2,3' union all
select 2,'b','1,2'if object_id('[TbIDs]') is not null drop table [TbIDs]
create table [TbIDs]([id] int,[IdName] varchar(2))
insert [TbIDs]
select 1,'我' union all
select 2,'你' union all
select 3,'他'
create function dbo.f_str(@s varchar(100)) returns varchar(100)
as
begin
select @s=replace(@s,cast(id as varchar(10)),[IdName]) from [TbIDs]
return @s
end
goselect id,name,dbo.f_str([tIDs]) from TB/*
id name
----------- ---- ----------------------------------------------------------------------------------------------------
1 a 我,你,他
2 b 我,你(2 行受影响)*/drop table [TbIDs]drop table [tb]
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)
-- 创建建表
create Table tb(id int,[name] varchar(10),tIDs varchar(20))
create Table TbIDs(id int, IdName nvarchar(10))
insert into tb select 1,'a','1,2,3'
union all select 2,'b','1,2'
insert into TbIDs select 1,'你'
union all select 2,'我'
union all select 3,'他'-- 创建函数
create function [dbo].[f_splitTb](@c varchar(2000),@split varchar(2))
returns varchar(100)
as
begin
declare @t table(col varchar(20))
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
declare @idName varchar(100)
SELECT @idName = isnull(@idName +',','')+convert(varchar(10),T.idName)
FROM (
select * from TbIDs
where id in (select * from @t)) as T return @idName
end -- 查询
select id,[name],dbo.f_splitTb(tb.tIDs,',')
from tb-- 结果
1 a 你,我,他
2 b 你,我