I know that the wording of the question sounds strange, but I could not in another way.

There is something like a query that looks for all elders, parents and childs.

SELECT `elders`.* FROM `elders` LEFT JOIN `parents` ON `elders`.`col` = `parents`.`elders_col` LEFT JOIN `childs` ON `parents`.`col` = `childs`.`parents_col` 

because using yii2, using the constructor looks like this:

 $model = Elders::find()->joinWith('parents.childs')->asArray()->all(); 

I also need to find all elders who may have a connection with parents, but parents cannot have a childs.

Help formulate such a request.

  • one
    And if they ask the fourth level, will you make another table? - u_mulder 3:07 pm
  • @u_mulder, maybe. and how do you propose to do? - dasauser

1 answer 1

Request selects only those elders who have parents without childs

 SELECT `elders`.* FROM `elders` JOIN `parents` ON `elders`.`col` = `parents`.`elders_col` LEFT JOIN `childs` ON `parents`.`col` = `childs`.`parents_col` WHERE `childs`.`id` is NULL