how to join query different table in laravel - laravel

I try to join three tables which are named attendances, categories, and users.
my code is
$Attendance= DB::table('users')
->join('categories','users.U_category_id', '=', 'categories.id')
->select('users.*','categories.batch_name')->get();
but I also need to join attendances table where attendances column User_A_ID and users table id are the same. How can I do this?

You can try it with
$Attendance= DB::table('users')
->join('categories','users.U_category_id', '=', 'categories.id')
->join('attendances','users.id', '=', 'attendances.User_A_ID')
->select('users.*','categories.batch_name')->get();

Related

Join 2 Temp tables 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

MySql Query error in Laravel

I am using laravel 5.4. When trying to run
select * from `users` inner join `addprojects` on `users`.`emp_id` = `addprojects`.`emp_id` where `emp_id` = $emp_id)"
It produces:
"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'emp_id'
in where clause is ambiguous (SQL: select * from users inner join
addprojects on users.emp_id = addprojects.emp_id where
emp_id = $emp_id)"
$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')->where('emp_id', '$emp_id')->get();
emp_id in both tables , so can use users.emp_id or addprojects.emp_id
->where('users.emp_id', '=', $emp_id)
You have specified column name which exists in both tables, so use users.emp_id or addprojects.emp_id doesn't matter which one
Example:
$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')
->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')
->where('users.emp_id', '$emp_id')
->get();
Because both users and addprojects tables have same named field "emp_id".
You can change your code like following.
$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')
->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')
->where('users.emp_id', $emp_id)
->get();

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.

Resources