SQL 服务器:从一个 table 获取所有数据,但以第二个 table 列为条件
SQL Server : get all data from one table but condition on 2nd tables column
我有 3 tables Student
, Classes
和 StudentClasses
.
学生 table:
类 table:
学生类 table:
我正在尝试的是
select
c.ClassID, sc.StudentID, Title,
ClassFrom as ClassDate, ClassTo as ClassTime,
Duration, Type as SessionType, sc.Status as StatusJoin
from
Classes c
left join
StudentClasses sc on sc.ClassID = c.ClassID
where
sc.StudentID = '66919287-d63d-4b30-931f-30532b68c2f1' or
sc.StudentID IS NULL
这个returns:
我认为这是正确的,但是当我将其更改为:
where sc.StudentID = '5a22f025-ae64-49b8-9782-32bc2f1ccef6'
这个returns:
这应该 return 所有 4 类 学生 ID 和加入日期 null
这里是问题:我想要的是全部 类 而不是一些学生加入或不加入 如果加入则应该有 Studentid 和 JoiningDate 否则这些列可以为 null
在 ON 子句中使用您的条件而不是 Where
select c.ClassID,sc.StudentID,Title,ClassFrom as ClassDate,ClassTo as
ClassTime,Duration,Type as SessionType,sc.Status as StatusJoin
from Classes c
left join StudentClasses sc on sc.ClassID = c.ClassID
and (sc.StudentID = '66919287-d63d-4b30-931f-30532b68c2f1' or
sc.StudentID IS NULL)
select c.ClassID,sc.StudentID,标题,ClassFrom 为 ClassDate,ClassTo 为
ClassTime、Duration、Type 作为 SessionType,sc.Status 作为 StatusJoin
来自 类 c
left join Student类 sc on sc.ClassID = c.ClassID
也会有帮助!!!
我有 3 tables Student
, Classes
和 StudentClasses
.
学生 table:
类 table:
学生类 table:
我正在尝试的是
select
c.ClassID, sc.StudentID, Title,
ClassFrom as ClassDate, ClassTo as ClassTime,
Duration, Type as SessionType, sc.Status as StatusJoin
from
Classes c
left join
StudentClasses sc on sc.ClassID = c.ClassID
where
sc.StudentID = '66919287-d63d-4b30-931f-30532b68c2f1' or
sc.StudentID IS NULL
这个returns:
我认为这是正确的,但是当我将其更改为:
where sc.StudentID = '5a22f025-ae64-49b8-9782-32bc2f1ccef6'
这个returns:
这应该 return 所有 4 类 学生 ID 和加入日期 null
这里是问题:我想要的是全部 类 而不是一些学生加入或不加入 如果加入则应该有 Studentid 和 JoiningDate 否则这些列可以为 null
在 ON 子句中使用您的条件而不是 Where
select c.ClassID,sc.StudentID,Title,ClassFrom as ClassDate,ClassTo as
ClassTime,Duration,Type as SessionType,sc.Status as StatusJoin
from Classes c
left join StudentClasses sc on sc.ClassID = c.ClassID
and (sc.StudentID = '66919287-d63d-4b30-931f-30532b68c2f1' or
sc.StudentID IS NULL)
select c.ClassID,sc.StudentID,标题,ClassFrom 为 ClassDate,ClassTo 为 ClassTime、Duration、Type 作为 SessionType,sc.Status 作为 StatusJoin 来自 类 c left join Student类 sc on sc.ClassID = c.ClassID
也会有帮助!!!