求查询语句。

todown 2010-10-11 02:17:16
A表
id(int) bid(nvarchar(max))
1 1,2,4
2 2,3

B表
id(int) name(nvarchar(max))
1 t1
2 t2
3 t3
4 t4

查询A表的时候,将A表的bid显示的内容替换成B表对应ID的Name内容,查询语句应该怎么写?

查询结果
1 t1,t2,t4
2 t2,t3

求教,谢谢。
...全文
78 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
todown 2010-10-11
  • 打赏
  • 举报
回复
好了,谢谢。
jwdream2008 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sqlcenter 的回复:]
SQL code
--> 测试数据:#a
if object_id('tempdb.dbo.#a') is not null drop table #a
create table #a(id int, bid varchar(8))
insert into #a
select 1, '1,2,4' union all
select 2, '2,3'
--> 测试数据:#b
if o……
[/Quote]
SQLCenter 2010-10-11
  • 打赏
  • 举报
回复
--> 测试数据:#a
if object_id('tempdb.dbo.#a') is not null drop table #a
create table #a(id int, bid varchar(8))
insert into #a
select 1, '1,2,4' union all
select 2, '2,3'
--> 测试数据:#b
if object_id('tempdb.dbo.#b') is not null drop table #b
create table #b(id int, name varchar(8))
insert into #b
select 1, 't1' union all
select 2, 't2' union all
select 3, 't3' union all
select 4, 't4'

select *, bname=stuff((select ','+name from #b where charindex(','+ltrim(id)+',',','+a.bid+',')>0 for xml path('')),1,1,'') from #a as a
/*
id bid bname
----------- -------- --------
1 1,2,4 t1,t2,t4
2 2,3 t2,t3
*/
jwdream2008 2010-10-11
  • 打赏
  • 举报
回复

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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