Different result for Laravel's Eloquent And Raw Query - laravel

Anyone experienced raw SQL queries having different results with eloquent?
Here is my eloquent returning a value:
BookingRoleSchedule::from('booking_role_schedules as a')
->whereExists(function ($q) {
return $q
->select(DB::raw(1))
->from('booking_role_schedules as b')
->where('a.id', '<>', 'b.id')
->whereRaw('not(a.start_date >= b.end_date or a.end_date <= b.start_date)');
})
->join('booking_role_schedule_talent as c', function($join) {
$join->on('a.id', '=', 'c.booking_role_schedule_id')
->whereExists(function($q) {
return $q
->from('booking_role_schedule_talent as d')
->select(DB::raw(1))
->where('c.booking_role_schedule_id', '<>', 'd.booking_role_schedule_id')
->whereRaw('not(c.user_id != d.user_id)');
});
})->get();
While my SQL query doesn't return any value
select *
from `booking_role_schedules` as `a`
inner join `booking_role_schedule_talent` as `c` on `a`.`id` = `c`.`booking_role_schedule_id`
and exists(select 1
from `booking_role_schedule_talent` as `d`
where `c`.`booking_role_schedule_id` <> `d`.`booking_role_schedule_id`
and not (c.user_id != d.user_id))
where exists(select 1
from `booking_role_schedules` as `b`
where `a`.`id` <> `b`.`id`
and not (a.start_date >= b.end_date or a.end_date <= b.start_date))

Related

Laravel Query Builder: Using `Case` with `Joins`

I am trying to replicate the following psql query in laravel:
SELECT
DISTINCT
I.id,
T.id,
T.name,
I.first_name,
I.middle_name,
I.last_name,
I.dob,
CASE WHEN NOT E.email IS NULL THEN '<<' ELSE '' END
FROM individuals AS I
LEFT JOIN titles AS T ON I.title_id = T.ID
LEFT JOIN individuals_emails_map AS IEM ON IEM.individual_id = I.id
LEFT JOIN emails AS E ON E.id = IEM.email_id;
When I use the query builder and write:
DB::table('individuals AS I')
->join('titles AS T', 'T.id', '=', 'I.title_id')
->leftjoin('individuals_emails_map AS IEM', 'IEM.individual_id', '=', 'I.id')
->leftjoin('emails AS E', 'E.id', '=', 'IEM.email_id')
->distinct('I.title_id', 'T.name', 'I.first_name', 'I.middle_name', 'I.last_name', 'I.dob')
->select('T.id','T.name', 'I.first_name','I.middle_name','I.last_name','I.dob')
->get();
... it works fine, however when I try and insert the CASE line:
DB::table('individuals AS I')
->join('titles AS T', 'T.id', '=', 'I.title_id')
->leftjoin('individuals_emails_map AS IEM', 'IEM.individual_id', '=', 'I.id')
->leftjoin('emails AS E', 'E.id', '=', 'IEM.email_id')
->distinct('I.title_id', 'T.name', 'I.first_name', 'I.middle_name', 'I.last_name', 'I.dob')
->select('T.id','T.name', 'I.first_name','I.middle_name','I.last_name','I.dob',
DB::raw("CASE WHEN NOT E.email IS NULL THEN '<<' ELSE '' END"))
->get();
... I get the following error:
Undefined table: 7 ERROR: missing FROM-clause entry for table "e"
What am I doing wrong?
I think that the problem in your case statement:
DB::raw("CASE WHEN NOT E.email IS NULL THEN '<<' ELSE '' END"))
it should be:
DB::raw('case WHEN E.email is Not NULL THEN "<<" Else " " END resultEmail'))

subquery inside orWhere in laravel

I need assistance to build up the query like below in laravel:
SELECT *
FROM table t
WHERE t.a = 1
OR (t.a=0
AND t.id IN (
SELECT o.a_id
FROM other_table o
WHERE o.x > 3
)
);
You could try to build your exact current query, and in fact it might even be the most efficient to write it. But, if we rephrase your query using a left join, it becomes somewhat easier to express in Laravel code.
SELECT *
FROM your_table t
LEFT JOIN other_table o
ON t.id = o.a_id AND o.x > 3
WHERE
t.a = 1 OR
(t.a = 0 AND o.a_id IS NOT NULL);
This would translate to the following Laravel code:
$result = DB::table('your_table t')
->leftJoin('other_table o', function($join) {
$join->on('t.id', '=', 'o.a_id');
$join->on('o.x', '>', '3');
})
->where('t.a', '=', '1')
->orWhere(function($query) {
return $query->where('t.a', '=', '0')
->whereNotNull('o.a_id')
})
->get();

Eloquent Laravel where with now

How can I transform this query to eloquent or dbquery in laravel?
SELECT
fases.*,
processos.processonome
FROM
fases
INNER JOIN
processos ON processos.processoid = fases.processo_id
WHERE
fases.arquivada = 0
AND DATEDIFF( fases.avisodata, NOW() ) <= fases.avisodias
ARCHIVED RESULT WITH
DB::table('fases')
->join('processos', 'fases.processo_id', '=', 'processos.processoid')
->where('fases.arquivada', 0)
->whereRaw('DATEDIFF(avisodata, NOW()) <= avisodias')
->select('fases.*', 'processos.processonome')
->get();
I hope this will work
DB::table('fases')
->join('processos', 'fases.processo_id', '=', 'processos.processoid')
->where('fases.arquivada', 0)
->where(DB::raw('DATEDIFF( fases.avisodata, NOW() )'), '<=', 'fases.avisodias')
->select('fases.*', 'processos.processonome')
->get();

SELECT COUNT in WHERE CLAUSE in Eloquent

I know that something similar was here some time ago,but it wasn't the same case and I can't just figure it out on myself.
I need to transform raw SQL query to Eloquent.
This query contains SELECT COUNT in WHERE clause, for simplicity I have this (may has not much sense) query :
SELECT u.column1, u.column2, u.column3, s.column1 FROM users u
LEFT JOIN salary s ON u.id = s.user_id
WHERE
(
SELECT count(cars_id) FROM cars WHERE cars.user_id = u.id
) = 0
AND u.city IN ("London","Paris")
I tried:
$columns = [
'users.column1',
'users.column2',
'users.column3',
'salary.column1'
];
$q = User::select($columns)
->leftJoin('salary', 'salary.user_id', '=', 'users.id')
->whereRaw(" (SELECT COUNT(cars_id) FROM cars WHERE cars.user_id = u.id) = 0 ")
->whereRaw("u.city IN ('London','Paris')")
->get();
But it doesn't return same results as raw SQL (SQL had 161 rows and Eloquent 154 rows).
Maybe you know how to transform this kind of query correctly to Eloquent?
Thanks
Based on your query I believe this should do it:
DB::table('users as u')
->select([ 'u.column1', 'u.column2', 'u.column3', 's.column1'])
->leftJoin('salary as s', 'u.id', '=', 's.user_id')
->leftJoin('cars as c', 'c.user_id', '=', 'u.id')
->whereIn('u.city', ['London', 'Paris'])
->havingRaw('count(c.id) = 0')
->get();
Please let me know.

how to create select join multiple condition in laravel 5.3

how to create select join multiple condition in laravel 5.3
SQL SELECT Statement.
SELECT table_1.column_1
,table_2.column_1
,table_3.column_1
FROM table_1
LEFT JOIN table_2
ON table_1.column_1 = table_2.column_1
LEFT JOIN table_3
ON table_1.column_2 = table_3.column_2
AND table_3.column_3 <= NOW()
AND ( table_3.column_4 >= NOW()
OR table_3.column_4 = 0
)
WHERE table_1.column_1 = '0000000001'
I want to convert SQL Statement to laravel select.
I try.
$result = DB::table('table_1')
->select('table_1.column_1', 'table_2.column_1', 'table_3.column_1')
->leftJoin('table_1', 'table_1.column_1', '=', 'table_2.column_1')
->leftJoin('table_3', 'table_1.column_2', '=', 'table_3.column_2')
->where('table_1', $_POST['id'])
->get();
DB::table('table_1')
->select('table_1.column_1', 'table_2.column_1', 'table_3.column_1')
->leftJoin('table_1', 'table_1.column_1', '=', 'table_2.column_1')
->leftJoin('table_3', 'table_1.column_2', '=', 'table_3.column_2')
->where('table_1.column_1', $_POST['id'])
->where('table_3.column_3','>=', NOW())
->where(function($query) {
$query->where('table_3.column_4', '>=', NOW())
->orWhere('table_3.column_4','0');
})
->get();

Resources