どのように私は、親テーブルのIDに関連するすべての孫を得ることができます

ワンBuyu:

私は親IDが定義されているすべての孫を取得したい、関連する3つのテーブルを持っています。私は、MySQLとPHPを使用しています。

        --------- RELATIOSHIP -------
        Parent has many child
        Child has many grandchild


        -------Parent table------
        | parentId | otherAttr  |
        -------------------------

        ------------ Child table ---------------
        |  childId  |  parentId  |  otherAttr  |
        ----------------------------------------

        ------------ grandChild table ----------
        |  grandId  |  childId  |  otherAttr  |
        ----------------------------------------
Mureinik:

参加するのカップルは、トリックを行う必要があります。

SELECT g.*
FROM   grandchild g
JOIN   child c ON g.childId = c.childId
JOIN   parent p ON c.parentId = p.parentId
WHERE  p.parentId = <somevalue>

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=284210&siteId=1