SELECT *
FROM `customer_session` AS `s`
LEFT JOIN `visit` AS `v`
ON `v`.`session_id` = `s`.`session_id`
AND `v`.`customer_id` = `s`.`customer_id`
AND `v`.`date` = '2022-05-06'
WHERE `s`.`session_id` = 1
Почему? потому что этот запрос как бы нужно дополнить в работающем приложении.
Мне кажется нужно как то через UNION делать.
//Laravel
$query = WorkshopInstanceVisit::query();
$query->join('workshop_session AS ws', 'workshop_instance_visit.workshop_session_id', '=', 'ws.workshop_session_id')
->leftJoin('customer_workshop_instance AS cwi', function ($join) {
$join->on('cwi.workshop_instance_id', '=', 'ws.workshop_instance_id');
});
$query->rightJoin('workshop_session AS ws', 'workshop_instance_visit.workshop_session_id', '=', 'ws.workshop_session_id')
? 2 rightJoin пробовал. Тоже не помогло.
$query = WorkshopInstanceVisit::query();
//Для конретного кружка делаем запрос
$query->where("workshop_instance_visit.workshop_session_id", "b57649c6-81ea-4f4c-bfa4-f91e2aed9ff3");
$query->select(
'workshop_instance_visit.*',
'workshop_instance_visit.customer_id as workshop_instance_visit_customer_id',
'cwi.customer_id'
);
$query->join('workshop_session AS ws', 'workshop_instance_visit.workshop_session_id', '=', 'ws.workshop_session_id');
// $query->leftJoin
$query->rightJoin('customer_workshop_instance AS cwi','cwi.workshop_instance_id', '=', 'ws.workshop_instance_id');
$worksshopInstanceVisits = $query->get();
Мой код.
$query->rightJoin('workshop_session AS ws', 'workshop_instance_visit.workshop_session_id', '=', 'ws.workshop_session_id')
Ваш код
$query->rightJoin('customer_workshop_instance AS cwi','cwi.workshop_instance_id', '=', 'ws.workshop_instance_id');
DB::enableQueryLog();
//ваши запросы
dd(DB::getQueryLog());
select
"workshop_instance_visit".*,
"workshop_instance_visit"."customer_id" as "workshop_instance_visit_customer_id",
"cwi"."customer_id"
from "workshop_instance_visit"
inner join "workshop_session" as "ws" on "workshop_instance_visit"."workshop_session_id" = "ws"."workshop_session_id"
right join "customer_workshop_instance" as "cwi" on "cwi"."workshop_instance_id" = "ws"."workshop_instance_id"
where "workshop_instance_visit"."workshop_session_id" = 'b57649c6-81ea-4f4c-bfa4-f91e2aed9ff3'
select
"workshop_instance_visit".*,
"workshop_session".*
from "workshop_instance_visit"
right join "workshop_session" as "ws" on "workshop_instance_visit"."workshop_session_id" = "ws"."workshop_session_id"
where "workshop_instance_visit"."workshop_session_id" = 'b57649c6-81ea-4f4c-bfa4-f91e2aed9ff3'
select
"workshop_instance_visit".*,
"workshop_session".*
from "workshop_instance_visit"
right join "workshop_session" as "ws" on "workshop_instance_visit"."workshop_session_id" = "ws"."workshop_session_id"
where "ws"."workshop_session_id" = 'b57649c6-81ea-4f4c-bfa4-f91e2aed9ff3'