I need to generate the following sql query

SELECT `searchjur`.*, `conditions`.* FROM `searchjur` LEFT JOIN `conditions` ON `searchjur`.`id` = `conditions`.`id_event` WHERE `conditions`.`id_jur` = 59 OR NOT EXISTS (SELECT * FROM searchjur WHERE `conditions`.`id_event` = `searchjur`.`id`) 

In yii2, I do the following

 $query = Searchjur::find()->select('searchjur.*, conditions.*'); $query->joinWith(['conditions']); $query->where(['not exists', Searchjur::find() ->where(['`conditions`.`id_event`' => 'searchjur.id'])]); $query->andWhere(['conditions.id_jur' => $id_jur]); return $query->createCommand()->getRawSql() ; 

The problem occurs with quotes I need at the output

enter image description here

a yii makes 'searchjur.id' single quotes not slashes

Professionals please tell me! Below is the entire query result in yii2.

  "SELECT `searchjur`.*, `conditions`.* FROM `searchjur` LEFT JOIN `conditions` ON `searchjur`.`id` = `conditions`.`id_event` WHERE (NOT EXISTS (SELECT * FROM `searchjur` WHERE `conditions`.`id_event`='searchjur.id')) AND (`conditions`.`id_jur`=59)" 

    2 answers 2

    Well, change

     ->where(['`conditions`.`id_event`' => 'searchjur.id']) 

    on this for example

     ->where('`conditions`.`id_event` = `searchjur`.`id`'); 

    because $ condition is string | array | Expression

    • Thanks for your reply. - Victor Vasilyev

    He formulated the entire query in string with all the punctuation I needed and inserted it into where (). It works

    $ query = Searchjur :: find () -> select ('searchjur. , conditions. '); $ query-> joinWith (['conditions']);

      $query->where(['conditions.id_jur' => $id_jur]); $query->orWhere('NOT EXISTS ( SELECT * FROM searchjur WHERE `conditions`.`id_event` = `searchjur`.`id` )');