R
R
Ruslan Ruslanov2019-04-02 16:20:21
Yii
Ruslan Ruslanov, 2019-04-02 16:20:21

How to find all ancestors without grandchildren?

I know that the wording of the question sounds strange, but I couldn't help it.
there is something like this 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 I use yii2, then using the constructor looks like this:
$model = Elders::find()->joinWith('parents.childs')->asArray()->all();

I need to find all the elders who may have a relationship with parents, but parents should not have childs.
help to formulate such a request.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Ruslanov, 2019-04-03
@dasauser

you just need to add to the current query WHERE `childs`.`parents_col` IS NULL
looks like this (sql):
yii2:

Elders::find()->joinWith('parents.childs')->where(['childs.parents_col' => null])->asArray()->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question