Jichao Jichao - 7 months ago 64
MySQL Question

Yii2 how to translate cascade select in where clause to activequery?

Two tables

, I need to dump all the unanswered questions for specific user. In sql

select * from question where question.user_id = user_id
and (select count(*) from answer where answer.question_id = question.id) = 0

But how could I turn this command to activequery?

Question::find()->where(['user_id' => user_id]) ....


An active query to find each question for a given user with no answer.

Question::find()->leftJoin('answer', 'answer.question_id=question.id')
                ->where(['question.user_id' => $user_id])
                ->andWhere(['answer.id' => null)