34,872
社区成员
发帖
与我相关
我的任务
分享
declare @stu table(stu_id int)
insert into @stu
select 001 UNION ALL
select 002 UNION ALL
select 003
declare @sc table( stu_id int,id int)
insert into @sc
select 001,1 UNION ALL
select 001,2 UNION ALL
select 001,3 UNION ALL
select 002,1 UNION ALL
select 002,2 UNION ALL
select 003,1 UNION ALL
select 003,2 UNION ALL
select 003,3 UNION ALL
select 003,4
select distinct a.stu_id from @stu as a inner join @sc as b
on a.stu_id=b.stu_id
where (select count(1) from @sc where stu_id=a.stu_id)>1
and exists(select * from @sc where stu_id=a.stu_id and id=1)
and exists(select * from @sc where stu_id=a.stu_id and id=3)
可以
declare @sc table([学号] varchar(3) ,课程编号 varchar(3) ,成绩 int)
insert @sc
select '001',1,50 union all
select '001',2,60 union all
select '001',3,80 union all
select '002',1,30 union all
select '002',3,90 union all
select '003',1,100 union all
select '003',2,100union all
select '004',2,100 union all
select '004',3,100
------------查询开始-------------
select a.学号 from
@sc a
join @sc b on a.学号=b.学号
where a.课程编号=1 and b.课程编号=3
------------结果---------------
学号
----
001
002--> 测试数据:@sc
declare @sc table([学号] varchar(3) ,课程编号 varchar(3) ,成绩 int)
insert @sc
select '001',1,50 union all
select '001',2,60 union all
select '001',3,80 union all
select '002',1,30 union all
select '002',3,90 union all
select '003',1,100 union all
select '003',2,100
select distinct 学号 from @sc sc1
where not exists(select 1 from (select 1 as 编号 union all select 3) m
where not exists(select 1 from @sc where 学号=sc1.学号 and 课程编号=m.编号 ) )
/*
学号
----
001
002
(2 行受影响)
*/

