父子链的外连接
考虑下面的表格和关系:
父级 --1:Many-- 子级 --1:Many-- subchildren
- 父级可能有也可能没有子级 记录。
- 孩子总是有子孩子记录。
我想编写一个查询来选择任何匹配的父名称 父名称、子名称或子名称。
在这里,我知道我必须在父母和孩子之间进行左外连接。但是我应该在孩子和子孩子之间建立什么样的连接呢?
Considering below tables and relationships:
parent --1:Many-- children --1:Many-- subchildren
- Parent may or many not have children
records. - children always have subchildren records.
I want to write a query to select parent names where any if matched
parent.name,children.name or subchildren.name.
Here I understand I have to do a left outer join between parent and children. But what kind of join should I put between children and subchildren ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解你的问题,这有效:
编辑(被接受后)添加:我实际上也会使用
OUTER
作为第二个JOIN
,以防万一规则在没有警告的情况下发生变化。如果不需要的话就不会造成伤害。If I understand your question, this works:
Edited (after being accepted) to add: I would actually use
OUTER
for the secondJOIN
as well, just in case the rules change without warning. It won't hurt if it's not needed.如果孩子总是有子孩子,你应该使用 INNER JOIN
If children always have subchildren you should use INNER JOIN
此查询将返回所有父级(包含或不包含子级),并在那些没有任何子级的父级的列中返回空值。
如果您要将 Children 和 SubChildren 之间的 JOIN 更改为 INNER
那么您将不会从没有任何孩子的父母那里获得行。
This query will return all parent (with or withour children), and return nulls in the columns for those parents that do not have any children.
If you were to change the JOIN between Children and SubChildren to INNER
Then you will not get the rows from parents that do not hav any children.