YII2。在连接中进行子选择。 '='而不是'in'

时间:2017-08-10 10:31:13

标签: yii2

我的查询如下:

SELECT * FROM `tasks` as t
LEFT JOIN `task2status` as t2s ON t2s.id = (SELECT t2s1.id FROM task2status as t2s1 WHERE t2s1.task_id = t.id ORDER BY t2s1.id  DESC LIMIT 1)

在搜索模型中:

$query = Tasks::find()->alias('t');
$subQuery = Task2status::find()->alias('t2s')->select('t2s.id')->where(['t2s.task_id' => 'tasks.id'])->orderBy('t2s.id desc')->limit(1)->offset(0);
$query->InnerJoin('task2status ts', ['ts.id' => $subQuery]);

结果,我得到以下

SELECT COUNT(*) FROM `tasks` `t` INNER JOIN `task2status` `ts` ON `ts`.`id` IN (SELECT `t2s`.`id` FROM `task2status` `t2s` WHERE `t2s`.`task_id`='tasks.id' ORDER BY `t2s`.`id` DESC LIMIT 1)

'限制'不能用于'in'。因此,发生错误 我需要得到'='而不是'in'。怎么弄这个?我究竟做错了什么? 提前谢谢你,对不起的英语抱歉。

1 个答案:

答案 0 :(得分:0)

如果限制为1,则不需要' in'。请将限制更改为2并检查