D
D
Dmitriy G2019-03-18 00:14:24
Yii
Dmitriy G, 2019-03-18 00:14:24

How to write LEFT JOIN ON ..OR in yii2?

There is a working request:

SELECT
  COUNT(`user`.`id`) AS `count`,
  `user`.`id` AS `id`,
  `user`.`username` AS `username`,
  `user`.`email` AS `email`,
  `user`.`FIO` AS `FIO`,
  `user`.`status` AS `status`,
  `user`.`phone` AS `phone`
FROM (`user`
  LEFT JOIN `event` `e`
    ON (((`user`.`id` = `e`.`id_operator`)
    OR (`user`.`id` = `e`.`id_operator2`)
    OR (`user`.`id` = `e`.`id_journalist`)
    OR (`user`.`id` = `e`.`id_driver`))))
WHERE (`e`.`status` = 1)
GROUP BY `user`.`username`,
         `user`.`email`,
         `user`.`FIO`,
         `user`.`status`,
         `user`.`phone`

Select and count how many employee records there are.
Interested in how to write multiple LEFT JOIN OR ??
doesn't work like that
...
   ->joinWith('operator',true,'LEFT JOIN')
   ->joinWith('journalist jour',true,'LEFT JOIN')
...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitriy G, 2019-03-18
@Dimgol

$newquery = User::find()
 ->select(['username',new Expression('count(id) as id')])
 -> leftJoin('event', 'event.id_operator=user.id  OR event.id_driver=user.id OR event.id_operator2=user.id  OR event.id_journalist=user.id')
            ->where(['event.status'=>1])
            ->groupBy(['username'])
            ->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question