首页 > 解决方案 > 单个 mysql 查询以显示祖父母父母的姓名和工作

问题描述

我有一个查询有问题,我有 3 个表

人(id 主键,姓名)

parent (id_of_child, id_of_father, id_of_mother) 都是外键 (id)

工作(person_id 外键、job_title、start_date、end_date)

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

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

然后我还需要扩展这个查询来获取这个孩子的父母和祖父母,最后是祖父母的父母的姓名和他们可能拥有的工作。所以我应该只有8个人的名字来自父亲和母亲的一面。

Parent 表是一种关系表,其中包含谁是父亲和母亲的数据。但我不太明白我应该如何使用这张桌子。或者如何在单个查询中挖掘到祖父母的父母。

我是 sql join 的新手,想通透这个问题非常困难。我在这些表中有一些随机数据,每行大约 10 行或更多。

我曾尝试从 google 研究 sql joins 并自己考虑这一点,但我坚持这一点。

标签: mysqlsqljoinselect

解决方案


您可能应该阅读更多关于数据库连接的内容。

AJOIN根据ON谓词组合两个表(A 和 B)的列值。有不同的类型JOIN。如果没有指定其他内容,通常INNER JOIN使用 an。这意味着结果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所有列并产生两行,每一行对应一个idinperson匹配id_of_childin 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    |
+-------------+--------------+--------------+

我让你来添加祖父母和工作,你应该可以从这里开始。


推荐阅读