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