我有一个表的字段为
A表
id value
1 1,2
2 2,3
3 1,3
另一个表是对应value的数据
B表
id name
1 书
2 电脑
3 笔记本我想得到
C表
id value
1 书,电脑
2 电脑,笔记本
3 书,笔记本这种该怎么办呢?
A表
id value
1 1,2
2 2,3
3 1,3
另一个表是对应value的数据
B表
id name
1 书
2 电脑
3 笔记本我想得到
C表
id value
1 书,电脑
2 电脑,笔记本
3 书,笔记本这种该怎么办呢?
解决方案 »
- 查询表A更新到表B
- 根据传入参数,返回2个关联表中的某列值
- 简单更新
- SQL中如何取得系统时间和日期(时间要求返回6位,比如152910;日期要求返回8位,比如20070517)
- 合并不同数据库中相同字段结构的表中的数据应该怎样做?
- delete和truncate的区别?
- 如何用sql解决这个问题
- 合并问题!难!
- vc与access数据库开发中的重大难题!多方求教均无果!请高手赐教!
- ajax 查询sql 数据库响应时间过长的问题
- 定时事务复制,为什么设置了在每天的某个时间点执行,到了时间订阅服务器没有获得更新呢?[
- -------------字段格式问题?……(*¥%%&……*)%¥¥%()*()*¥@@##%¥%
if object_id('tempdb.dbo.#ta') is not null drop table #ta
go
create table #ta (id int,[value] varchar(6) )
insert into #ta
select 1,'1,2' union all
select 2,'2,3' union all
select 3,'1,3'
--> 测试数据: #tB
if object_id('tempdb.dbo.#tB') is not null drop table #tB
go
create table #tB (id int,name varchar(6))
insert into #tB
select 1,'书' union all
select 2,'电脑' union all
select 3,'笔记本';with cte as
(
select a.id,b.name
from #ta a,#tb b
where charindex(','+ltrim(b.id)+',',','+a.[value]+',')>0
)select id ,
name=stuff( (select ','+name from cte where id=t.id for xml path('')),1,1,'')
from cte t
group by idid name
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 书,电脑
2 电脑,笔记本
3 书,笔记本(3 行受影响)
drop table ta
Go
Create table ta([id] int,[value] nvarchar(3))
Insert ta
select 1,N'1,2' union all
select 2,N'2,3' union all
select 3,N'1,3'
Go
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[name] nvarchar(3))
Insert tb
select 1,N'书' union all
select 2,N'电脑' union all
select 3,N'笔记本'
Go
select ID,
[value]=stuff((select ','+[name]
from tb b
where charindex(','+ltrim(ID)+',',','+a.[value]+',')>0
for xml path('')),1,1,'')
from ta a
/*
ID value
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 书,电脑
2 电脑,笔记本
3 书,笔记本(3 row(s) affected)
*/
use City;
go
if OBJECT_ID(N'A',N'U') is not null drop table A
go
if OBJECT_ID(N'B',N'U') is not null drop table B
go
create table A
(
id int identity(1,1) primary key not null,
value nvarchar(5)
)
go
create table B
(
id int identity(1,1) primary key not null,
name nvarchar(5)
)
go
--插入A表测试数据
insert into A
select '1,2' union all
select '2,3' union all
select '1,3'
go
--插入B表测试数据
insert into B
select '书' union all
select '电脑' union all
select '笔记本'
go
with cte1 as
(
select ATable.AID,BTable.BID from
(
select cast('<V>'+replace(value,',','</V><V>')+'</V>' as xml)as ROW,id as AID
from A
)as ATable
outer apply
(
select
C.value('.','int') As BID
from ATable.ROW.nodes('/V') T(C)
)as BTable
)
select distinct AID,
stuff
((select ','+name from B where id in
(select BID from cte1 as twocte where onecte.AID=twocte.AID)
for XML path('')),1,1,'')as myname from cte1 as onecte
drop table A
drop table B
/*(3 行受影响)(3 行受影响)
AID myname
----------- --------------
1 书,电脑
2 电脑,笔记本
3 书,笔记本(3 行受影响)
*/