Join 2 Temp tables laravel - laravel

I have following temp tables I wanna join them In query builder but I am failing to do so. I wanna join these 2 tables so I can do i1.imp/i2.imp
$subQuery1 = MyModel::query()
->from("table as i1")
->select(
\DB::raw('sum(col) as col1'),
\DB::raw('co1')
)->where('stamp', '>=', '2022-03-01 14:25:00')
->where('stamp', '<', '2022-03-07 14:30:00')
->groupBy(
'co1'
);
$subQuery2 = MyModel::query()
->from("table as i2")
->select(
\DB::raw('sum(col) as col1'),
\DB::raw('co1')
)->where('stamp', '>=', '2022-03-01 14:20:00')
->where('stamp', '<', '2022-03-07 14:25:00')
->groupBy(
'co1'
);

You can use from, fromSub or table and pass in a subquery instead of a table.
You can do the same with joinSub for joins.
You need to provide an alias though.
For example, to use $subquery1 as the main table and join it with $subquery2, the resulting query could look like this:
$results = DB::query()
->select(.....)
->fromSub($subquery1, 'i1')
->joinSub($subquery2, 'i2', function ($join) {
$join->on('i1.col', '=', 'i2.col');
// ->orOn(....)
});
->where(....)
->get();
Laravel 9.x API - fromSub
Queries - Subquery Joins

Related

left join in laravel return null if data exist in database

I'm trying to left join two tables in laravel. after joining got all second table value is empty but data exist in a database .
Can you try this:
$contacts = DB::table('C_Contacts')
->leftJoin('C_Contact_Category_Map', 'C_Contacts.contact_id', '=', 'C_Contact_Category_Map.contact_id')
->get();
Can You try this
$contacts = DB::table('C_Contacts')
->join('C_Contact_Category_Map', 'C_Contacts.contact_id', '=', 'C_Contact_Category_Map.contact_id')
->get();

Laravel Left Join Query

I am using laravel 5.3 and I have some left join query with error in laravel query method.
This is my normal query
SELECT bran.branchName,sch.schoolName From m_schoolbranch bran
LEFT JOIN m_students stu ON stu.schoolNo=bran.schoolNo AND stu.branchNo=bran.branchNo
LEFT JOIN m_school sch ON sch.schoolNo=stu.schoolNo where stu.userNo='0000000001';
And this is my new laravel Query
DB::table('m_schoolbranch')
->join('m_students', 'm_schoolbranch.schoolNo', '=', 'm_students.schoolNo')
->join('m_students', 'm_schoolbranch.branchNo', '=', 'm_students.branchNo')
->join('m_school', 'm_schoolbranch.schoolNo', '=', 'm_school.schoolNo')
->select('m_school.schoolName', 'm_schoolbranch.branchName')
->where('m_students.userNo',$userNo)
->get();
In these query I need to match two column in table m_students so I put like this
->join('m_students', 'm_schoolbranch.branchNo', '=', 'm_students.branchNo')
But i show error...
Tables in the query need to have unique names, otherwise the DB has no way of knowing which m_schoolbranch should be used when evaluating m_schoolbranch.schoolNo.
You could use unique table aliases in your join statements but I recommend using multiple conditions on the join. Just like you use in your original SQL query. See here: https://stackoverflow.com/a/20732468/4437888
DB::table('m_schoolbranch')
->join('m_students', function($join)
{
$join->on('m_schoolbranch.schoolNo', '=', 'm_students.schoolNo');
$join->on('m_schoolbranch.branchNo', '=', 'm_students.branchNo');
})
->join('m_school', 'm_schoolbranch.schoolNo', '=', 'm_school.schoolNo')
->select('m_school.schoolName', 'm_schoolbranch.branchName')
->where('m_students.userNo',$userNo)
->get();

laravel execute subquery query and get count

how select query like this in Laravel -
Select t.*, count(Select * from persons person
where person.user_id = t.id) from users t
try:
DB::table('users')
->selectRaw('*, count(SELECT * FROM persons WHERE persons.user_id = users.id)')
->get();
In my opinion to count data of other table, you can use join table combining with group. It's same as your query. You can do this:
$users = DB::table('users')
->join('persons', 'users.id', '=', 'persons.user_id')
->select('users.*', count(persons.user_id))
->groupBy('persons.user_id')
->get();
I hope this can help you.

Wherein query to related tables

I have been trying to a query where in I want to do some validation on the related tables. I would like to do something like this using Laravel 5
select
*
from
table1
INNER JOIN table3 on(table1.id = table3.table1_id)
INNER JOIN table2 on(table1.id = table2.table1_id)
where
table2.column2 in ('arr1', 'arr2');
Also table1 is related to 5 - 7 tables and I want to eager load all of this. Here's what I had so far
$reports = Table1::with(
[
'table3',
'table4',
'table5.table6',
'table7',
'table8',
])
->where('created_at', '>=', date('Y-m-d', strtotime($request->get('from'))))
->where('created_at', '<', date('Y-m-d', strtotime($request->get('to'))))
->with('table2',function($query) use($table1_column){
return $query->whereIn('table1_column',$table1_column);
});
But this displays everything. Even the items that does not exist in table2. What I would like to achieve is to create a result where all items is the only items that exists in table2. Meaning all transaction made using items in table2.
Assuming the item in table2 has an ids of 123, 456, and 789 then I would like to display all record related to this id's
How can I make this kind of result
You can use the whereHas method.
$reports = Table1::with(
[
'table3',
'table4',
'table5.table6',
'table7',
'table8',
])
->whereHas('table2', function($query) use($table1_column){
return $query->whereIn('table1_column',$table1_column);
})
->where('created_at', '>=', date('Y-m-d', strtotime($request->get('from'))))
->where('created_at', '<', date('Y-m-d', strtotime($request->get('to'))));
Note that this does not include the table 2 data in the result set. If you need that include the table2 relation name in the with method call.

subquery with distinct clause in query builder/laravel

SELECT * FROM `movie_list`
WHERE `movie_id` IN
(SELECT DISTINCT movie_id FROM `movie_genre` where genre_id in (12,18,53))
AND rated IN
('Not Rated','N/A')
How can i convert the above to a query builder syntax:
$movies = DB::table('movie_list')
->whereIn('movie_id',function($query){
$query->select.....
})->get();
I have the inner one: it goes like this:
DB::table('movie_genre')
->whereIn('genre_id', array(12,18,53))
->distinct()
->get(array('movie_id'));
How do i use this result with the rest of my query?
You could do this alot smoother with Eloquent Models, but assuming you don't have your models setup, this should do the trick (untested)
$ids = DB::table('movie_genre')
->whereIn('genre_id', [12,18,33])
->distinct()
->get(['movie_id'])
->toArray();
$movies = DB::table('movie_list')
->whereIn('movie_id', array_values($ids))
->get();

Resources