查找 children 中的 parents
Finding grand parents of children
这几天我一直在努力了解内部联接的工作原理。
我有两个简单的表格(亲情):
- “人”(id、fname、lname)
- “关系”(parent/child)
我学会了如何获得所有 parents of children
SELECT p.firstname, p.lastname, c.firstname, c.lastname
FROM persons p
INNER JOIN relationships ON p.id = relationships.parent
INNER JOIN persons c ON c.id = relationships.child;
获得 children 的全部 parents 怎么样?
它只是对该查询的调整还是涉及更多?
在我看来,我应该对查询应用相同的查询(一次性递归),但我不知道该怎么做。
感谢您的指导。
PS:内部连接在纸面上很容易理解,但对我来说很难使用,因为我习惯了过程算法来创建脚本,但内部连接不是过程的完全以他们应该接近的方式。所以我正在尝试分析用例以适应它们
What about getting all the grand parents of children instead?
首先是您的查询,但使用更好的关系名称:
SELECT p.firstname, p.lastname, c.firstname, c.lastname
FROM
relationships p_c
INNER JOIN persons p ON p.id = p_c.parent
INNER JOIN persons c ON c.id = p_c.child;
然后要获得 grandparent:child 你必须通过父路由:
SELECT p.firstname, p.lastname, c.firstname, c.lastname
FROM
relationships p_c
INNER JOIN relationships gp_p ON gp_p.child = p_c.parent --in a grandparent:parent relationship the child of the grandparent, is the parent of the grandchild in the parent:child
INNER JOIN persons gp ON gp.id = gp_p.parent --parent = the grandparent
INNER JOIN persons c ON c.id = p_c.child; --child = the grandchild
不确定我是否会选择这个例子来学习内连接。此外,如果关系列被称为其他东西,比如 elder
和 younger
:)
这几天我一直在努力了解内部联接的工作原理。
我有两个简单的表格(亲情):
- “人”(id、fname、lname)
- “关系”(parent/child)
我学会了如何获得所有 parents of children
SELECT p.firstname, p.lastname, c.firstname, c.lastname
FROM persons p
INNER JOIN relationships ON p.id = relationships.parent
INNER JOIN persons c ON c.id = relationships.child;
获得 children 的全部 parents 怎么样?
它只是对该查询的调整还是涉及更多? 在我看来,我应该对查询应用相同的查询(一次性递归),但我不知道该怎么做。
感谢您的指导。
PS:内部连接在纸面上很容易理解,但对我来说很难使用,因为我习惯了过程算法来创建脚本,但内部连接不是过程的完全以他们应该接近的方式。所以我正在尝试分析用例以适应它们
What about getting all the grand parents of children instead?
首先是您的查询,但使用更好的关系名称:
SELECT p.firstname, p.lastname, c.firstname, c.lastname
FROM
relationships p_c
INNER JOIN persons p ON p.id = p_c.parent
INNER JOIN persons c ON c.id = p_c.child;
然后要获得 grandparent:child 你必须通过父路由:
SELECT p.firstname, p.lastname, c.firstname, c.lastname
FROM
relationships p_c
INNER JOIN relationships gp_p ON gp_p.child = p_c.parent --in a grandparent:parent relationship the child of the grandparent, is the parent of the grandchild in the parent:child
INNER JOIN persons gp ON gp.id = gp_p.parent --parent = the grandparent
INNER JOIN persons c ON c.id = p_c.child; --child = the grandchild
不确定我是否会选择这个例子来学习内连接。此外,如果关系列被称为其他东西,比如 elder
和 younger
:)