简体   繁体   English

如何在外部联接中为表编写条件?

[英]How to write a condition for a table in an outer join?

I would like to display all sessions with the date, which is taken from the schedule_session table by joining it, and filtering by class_id. 我想显示所有带有日期的会话,该日期通过加入schedule_session表并通过class_id.过滤而class_id.

select * from sessions s 
left join schedule_session ss on ss.session_id = s.id 
where ss.class_id = 5

This query does not return all sessions because I am saying where class_id = 5 so it returns only specific class sessions. 该查询不会返回所有会话,因为我说的是class_id = 5因此它仅返回特定的类会话。 Is there any method that I can first get all sessions and show in the date property, and the date from the schedule_session if it exists for that particular session. 有什么方法可以让我首先获取所有会话并显示在date属性中,以及是否来自schedule_session的日期(如果该日期存在于该特定会话中)。 So in the end I have all sessions but haven't assigned any date for it. 因此,最后我进行了所有会话,但尚未指定任何date

So I want all sessions and show the value of date if it exists for that class, if not show date as null or blank . 所以我想要所有会话并显示date的值(如果该类存在),如果不显示date为nullblank

Thanks 谢谢

Not clear description but i think below is your solution. 描述不明确,但我认为以下是您的解决方案。

select * from sessions s 
left join schedule_session ss on ss.session_id = s.id and ss.class_id = 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM