【问题标题】:Single mysql query to show grandparents parents names and jobs单个 mysql 查询以显示祖父母父母的姓名和工作
【发布时间】:2021-02-20 15:14:07
【问题描述】:

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

人(id 主键,姓名)

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

job(person_id 外键、job_title、start_date、end_date)

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

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

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

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

我是 sql 连接的新手,想清楚这一点非常困难。 我在这些表中有一些随机数据,每行大约 10 行或更多。

我曾尝试从 google 研究 sql joins 并自己思考这个问题,但我被困住了。

【问题讨论】:

    标签: mysql sql join select


    【解决方案1】:

    根据一个人的名字选择父母而不使用任何 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"
    

    【讨论】:

      【解决方案2】:

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

      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 包含两个源表的所有列并生成两行,person 中的每个 idparent 中的 id_of_child 匹配。

      您的结果将是:

      +-------------+--------------+--------------+----+-------------+
      | 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    |
      +-------------+--------------+--------------+
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        • 2020-07-25
        相关资源
        最近更新 更多