How can I get all the grandchild that relate to parent table id

One Buyu :

I have 3 tables that relate, I want to get all grandChild where the parent id is defined. I am Using mySql and 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 :

A couple of joins should do the trick:

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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=276826&siteId=1
Recommended