The request is as follows:

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) 

In the search model, the query looks like this:

 $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]); 

As a result, I get:

 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) 

It is necessary to return only 1 record from subselect, therefore limit used. Yii2 substitutes in instead of equality, and limit for in cannot be used. But on the one hand, this is understandable, if you consider what I am giving to him. But how else? How to be?

  • one
    And what if you use $db->createCommand(); and execute the query exactly as you need. :) - Stanislav Hmelevsky
  • id in (3) and id = 3 is the equivalent. Not? - Skywave

0