单个mysql查询以显示祖父母的父母的姓名和工作

时间:2020-11-08 14:20:11

标签: mysql sql join select

我有3个表的单个查询有问题

人(id主键,姓名)

父级(id_of_child,id_of_father,id_of_mother)都是外键(id)

职位(person_id外键,职位名称,开始日期,结束日期)

我应该有一个SELECT子句,该子句可以获取可以在查询中更改的孩子的名字。喜欢:

SELECT name FROM person WHERE id=id_of_child AND name='insert name'

然后,我还需要扩展此查询,以获取此孩子的父母和祖父母,最后是祖父母的父母的姓名以及他们可能从事的工作。因此,我应该只有8个人的名字从父母双方的名字中显示出来。

Parent表是一种关系表,其中包含谁是谁的父母的数据。但是我不太了解如何使用此表。或者如何在单个查询中挖掘祖父母的父母。

我是sql联接的新手,并且很难做到这一点。 这些表中有一些随机数据,每行大约10行或更多。

我试图研究来自google的sql联接,并独自思考,但是我对此深信不疑。

2 个答案:

答案 0 :(得分:0)

要根据一个人的名字选择没有任何JOIN的父母:

SELECT name FROM Person
WHERE id IN (
    SELECT id_of_father FROM Parent WHERE id_of_child=(SELECT id FROM Person WHERE name="Your desired name")
)
OR id IN (
    SELECT id_of_mother FROM Parent WHERE id_of_child=(SELECT id FROM Person WHERE name="Your desired name")
)

翻译成英文:

I want to know the name of Persons
Who are
    The father of the person with the name "Your desired name"
Or
    The mother of the person with the name "Your desired name"

答案 1 :(得分:0)

您可能应该阅读有关数据库联接的更多信息。

A JOIN根据ON谓词组合两个表(A和B)的列值。 JOIN有不同类型。如果未指定其他任何内容,则通常使用INNER JOIN。这意味着JOIN的结果仅包含行,这两个表的行在相应列中均具有匹配的列值。

如果您的表格例如看起来像这样

           +----+-------------+
person:    | id | name        |
           +----+-------------+
           |  1 | Alice       |
           |  2 | Bob         |
           |  3 | Alice's Mum |
           |  4 | Alice's Dad |
           |  5 | Bob's Mum   |
           |  6 | Bob's Dad   |
           +----+-------------+

           +-------------+--------------+--------------+
parent:    | id_of_child | id_of_father | id_of_mother |
           +-------------+--------------+--------------+
           |      1      |       4      |       3      |
           |      2      |       6      |       5      |
           +-------------+--------------+--------------+

要结合使用这两种方法,现在可以使用(INNER) JOIN

SELECT *
FROM parent  
INNER JOIN person
    ON person.id = parent.id_of_child;

这将为您提供两个表的组合。 INNER JOIN具有两个源表的所有列,并产生两行,id中的每个personid_of_child中的parent匹配。 >

您的结果将是:

+-------------+--------------+--------------+----+-------------+
| id_of_child | id_of_father | id_of_mother | id | name        |
+-------------+--------------+--------------+----+-------------+
|      1      |       4      |       3      |  1 | Alice       |
|      2      |       6      |       5      |  2 | Bob         |
+-------------+--------------+--------------+----+-------------+

由于您不再对加入的ID感兴趣,因此可以只选择所需的列子集(也可以选择使用AS关键字重命名它们):

SELECT person.name AS child, parent.id_of_father, parent.id_of_mother 
FROM parent 
INNER JOIN person
    ON person.id = parent.id_of_child;

导致:

+-------------+--------------+--------------+
| child       | id_of_father | id_of_mother |
+-------------+--------------+--------------+
| Alice       |       4      |       3      |
| Bob         |       6      |       5      |
+-------------+--------------+--------------+

像这样,您也可以多次连接表person来获取父亲和母亲的名字。

SELECT p1.name as child,
       p2.name as father,
       p3.name as mother
FROM parent 
INNER JOIN person p1
    ON p1.id = parent.id_of_child
INNER JOIN person p2
    ON p2.id = parent.id_of_father
INNER JOIN person p3
    ON p3.id = parent.id_of_mother;

给你

+-------------+--------------+--------------+
| child       | father       | mother       |
+-------------+--------------+--------------+
| Alice       | Alice's Dad  | Alice's Mum  |
| Bob         | Bob's Dad    | Bob's Mum    |
+-------------+--------------+--------------+

我留给您添加祖父母和工作,您应该能够从这里继续下去。

相关问题