SQL查询以获取已注册英语或乌尔都语类(class)但未同时注册的学生

标签 sql sql-server database

查询应返回 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/

相关文章:

sql-server - 是否可以从命令行运行 DTSWizard.exe?

mysql - 为复杂过滤创建sql索引

sql-server - 内部联接并将多个表分组为一行

mysql - 无法在 mysql(phpmyadmin) 中获取与 0 不同的右侧小数值

java - 如何检测Dataverse表记录的变化

sql-server - 将用户定义的类型从一个数据库复制到另一个数据库

MySQL 5.6 比 MySQL 8 更快

python - 保存实时数据帧的最佳方法是什么?

SQL:过滤类型 A 事件发生后 1 秒内未发生类型 B 事件的行

mysql - 如何在 Excel 单元格中保存 blob 文件