如何在yii2中更正此请求?

q3aa0525  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(201)

我需要使用\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(正确)

我得到了什么。见图2。不适合我

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题