鍍金池/ 問答/PHP/ yii2使用leftjoin如何生成on……and查詢語句

yii2使用leftjoin如何生成on……and查詢語句

$sql = Wrong::find()->where([
            't.fdUserID' => 4,
            't.fdStatus' => 0
        ])->alias('t')->leftJoin(Exercise::tableName().' exercise','exercise.id=t.fdExerciseID',['exercise.fdStatus'=>1]);
print_r($sql->createCommand()->getRawSql());

打印的sql語句為

SELECT `t`.* FROM `tbWrong` `t` LEFT JOIN `tbExercise` `exercise` ON exercise.id=t.fdExerciseID WHERE (`t`.`fdUserID`=4) AND (`t`.`fdStatus`=0)

請問下exercise.fdStatus=1這個條件為啥沒生成,還有如何生成期望的sql語句

SELECT `t`.* FROM `tbWrong` `t` LEFT JOIN `tbExercise` `exercise` ON (exercise.id=t.fdExerciseID AND exercise.fdStatus=1) WHERE (`t`.`fdUserID`=4) AND (`t`.`fdStatus`=0)
回答
編輯回答
掛念你
Wrong::find()
    ->where([
        't.fdUserID' => 4,
        't.fdStatus' => 0
    ])
    ->alias('t')
    ->leftJoin(
        Exercise::tableName().' exercise',
        'exercise.id=t.fdExerciseID and exercise.fdStatus=1'
    );
2018年3月28日 16:43