如何在Yii2中使此请求正确?

时间:2018-11-23 18:28:56

标签: mysql database yii2

我需要使用\ yii \ db \ Query()进行此请求。 我该怎么办?我有此请求,它在PHPMyAdmin中运行良好,但是无法使用\ yii \ db \ Query()重复此请求。这是我的SQL查询:

SELECT
    `z`.idzadaniya,
    `r`.id
FROM
    `zadaniya` AS `z`
LEFT JOIN
    `result` AS `r`
ON
    `r`.zadaniya_idzadaniya = `z`.idzadaniya AND `r`.created_at IN(
    SELECT
        MAX(`r`.created_at)
    FROM
        `zadaniya` AS `z`
    JOIN
        `result` AS `r`
    ON
        z.idzadaniya = r.zadaniya_idzadaniya
    GROUP BY
        `z`.idzadaniya
)

这是我要发出的请求,但是我无法使用\ yii \ db \ Query()来实现它。该请求返回的不是我所需的全部。它不返回空值,也不返回当前的$ subQuery。

 $subQuery = (new \yii\db\Query())->select(['max(r.created_at)'])->from('zadaniya AS z');

    return $rows = (new \yii\db\Query())
        ->select(['z.idzadaniya','r.id'])->from('zadaniya AS z')
        ->join('LEFT JOIN', 'result AS r', 'r.zadaniya_idzadaniya = z.idzadaniya')
        ->andWhere(['result.created_at'=>$subQuery])
        ->join('LEFT JOIN', 'result', 'z.idzadaniya = result.zadaniya_idzadaniya')
        ->groupBy('z.idzadaniya')
        ->all();

我想要得到什么。参见图1。(正确) SQL Query return this (correct)

我得到的。见图2。对我不正确 yii\db\Query return (not correct for me)

0 个答案:

没有答案