从 1 child table 中引用的 2 parent table 获取信息
Get information from 2 parent tables referenced in 1 child table
想象一下这些 tables
人
id name
1 John
2 Jane
3 Joe
4 Jill
事物
id name
1 Apple
2 Banana
3 Carrot
有
pid tid
1 1
1 2
2 3
其中 Has
是 Person
和 Thing
之间的 many-to-many 关系。我怎样才能获得所有 Person
和 Thing
有关系的原始 parent table 信息?
基本上是这样的:
pname tname
John Apple
John Banana
Jane Carrot
这听起来像是最基本的 SQL 事情之一,所以我知道这是一个基本问题,但我无法正确表达它,所以我一直无法找到答案正在搜索。
以下是我试过的查询。我知道这只是第一步,因为它涉及 Person
,但不确定如何获得 Things
以及
SELECT
*
FROM
person p
INNER JOIN has h
ON p.id = h.pid;
所以你继续:
SELECT . . . -- the columns you want
FROM person p INNER JOIN
has h
ON p.id = h.pid INNER JOIN
thing t
ON t.id = h.tid;
想象一下这些 tables
人
id name
1 John
2 Jane
3 Joe
4 Jill
事物
id name
1 Apple
2 Banana
3 Carrot
有
pid tid
1 1
1 2
2 3
其中 Has
是 Person
和 Thing
之间的 many-to-many 关系。我怎样才能获得所有 Person
和 Thing
有关系的原始 parent table 信息?
基本上是这样的:
pname tname
John Apple
John Banana
Jane Carrot
这听起来像是最基本的 SQL 事情之一,所以我知道这是一个基本问题,但我无法正确表达它,所以我一直无法找到答案正在搜索。
以下是我试过的查询。我知道这只是第一步,因为它涉及 Person
,但不确定如何获得 Things
以及
SELECT
*
FROM
person p
INNER JOIN has h
ON p.id = h.pid;
所以你继续:
SELECT . . . -- the columns you want
FROM person p INNER JOIN
has h
ON p.id = h.pid INNER JOIN
thing t
ON t.id = h.tid;