查询应返回 Naveed Rizwan Fayaz 和 Ahmed Name。
针对已注册英语或乌尔都语类(class)但未注册两者的学生的 SQL 查询。
declare @Student table(sid int identity(1, 1), sname varchar(250))
declare @Course table(cid int identity(1, 1), cname varchar(250))
declare @StudentCourse table(cid int, sid int)
insert into @Student(sname)
select 'Mehboob' union all --1
select 'Rahim' union all -- 2
select 'Naveed' union all --3
select 'Rizwan' union all --4
select 'Fayaz' union all --5
select 'Ahmed' -- 6
insert into @Course(cname)
select 'English' union all
select 'Urdu'
insert into @StudentCourse(sid ,cid)
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,1 union all
select 5,2 union all
select 6,2 union all
select 1,2 union all
select 2,2
最佳答案
我一直在练习,最后我通过这个查询做到了,你们还有其他想法吗?
select
s.*, count(1) as TotalEnrolledCourses
from
@Student s
inner join
@StudentCourse sc on s.sid = sc.sid
where
sc.cid in (1,2)
group by
s.sid, sname
having
TotalEnrolledCourses = 1
关于SQL查询以获取已注册英语或乌尔都语类(class)但未同时注册的学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32132762/