34,594
社区成员
发帖
与我相关
我的任务
分享
课程表
id 课程名称 报名人员
1 NET 790,780,417
2 c++培训 790,417,780
3 c培训 417,790,962,
分享表
课程id 工号 主题 心得
1 790 66666666 666666666
2 790 学习心得 这个课程真好
3 790 ff bucuo
我想得到的结果
课程id 工号
1 780
2 780
3 780
1 417
2 417
3 417
3 962
我来个折分的版本吧 我以id和v做课程号与工号
select w.id,b.x from ( select a.id,u=cast('<root><v>'+replace(a.v,',','</v><v>')+'</v></root>' as xml) from @t a) w outer apply (select x=N.v.value('.','varchar(10)') from w.u.nodes('/root/v') N(v)) b
except (select id,v from @t1)
create table 课程表
(id int, 课程名称 varchar(8), 报名人员 varchar(20))
insert into 课程表
select 1, 'NET', '790,780,417' union all
select 2, 'c++培训', '790,417,780' union all
select 3, 'c培训', '417,790,962'
create table 分享表
(课程id int, 工号 int, 主题 varchar(10), 心得 varchar(20))
insert into 分享表
select 1, 790, '66666666', '666666666' union all
select 2, 790, '学习心得', '这个课程真好' union all
select 3, 790, 'ff', 'bucuo'
select c.id '课程id',c.工号
from
(select a.id,
substring(a.报名人员,b.number,charindex(',',a.报名人员+',',b.number)-b.number) '工号'
from 课程表 a
inner join master.dbo.spt_values b
on b.[type]='P' and substring(','+a.报名人员,b.number,1)=',') c
left join
(select 课程id,工号 from 分享表) d
on c.id=d.课程id and c.工号=d.工号
where d.课程id is null and d.工号 is null
课程id 工号
----------- --------------------
1 780
1 417
2 417
2 780
3 417
3 962
(6 row(s) affected)
SELECT A.*
FROM 报名人员表 A
LEFT JOIN 分享表 B
ON A.课程ID=B.课程ID
AND A.工号=B.工号
WHERE B.课程ID IS NULL