left join with 2 columns - php

I want to join two tables with two columns,
It's working in phpmyadmin. But it's not working with laravel.
So how to convert it in laravel structure.
SELECT
`j`.*,
`ts`.*,
`ps`.*,
`pn`.*,
`c`.*,
`planner`.*,
`ps`.`name` AS `section_name`,
`ps`.`id` AS `section_id`,
`planner`.`date` AS `planner_date`,
`pn`.`id` AS `planner_note_id`
FROM
`planner`
INNER JOIN
`timeslots` AS `ts` ON `ts`.`id` = `planner`.`timeslot_id`
INNER JOIN
`planner_sections` AS `ps` ON `ps`.`id` = `planner`.`planner_section_id`
LEFT JOIN
`planner_notes` AS `pn` ON `pn`.`date` = `planner`.`date` and `pn`.`contract_id` = `planner`.`contract_id`
INNER JOIN
`contracts` AS `c` ON `c`.`id` = `planner`.`contract_id`
INNER JOIN
`jobs` AS `j` ON `j`.`contract_id` = `c`.`id`
WHERE
`c`.`id` = 57
AND MONTH(`planner`.`date`) = 6
AND `planner`.`date` >= '2017-06-13'
AND `planner`.`deleted_at` IS NULL
ORDER BY `planner`.`date` ASC
But when i put it in laravel query structure it's not working.
$planner_list = Planner::join("timeslots as ts","ts.id","planner.timeslot_id")
->join("planner_sections as ps","ps.id","planner.planner_section_id")
->leftJoin("planner_notes as pn","pn.date","planner.date")
->leftJoin("planner_notes as pn","pn.contract_id","planner.contract_id")
->join("contracts as c","c.id","planner.contract_id")
->join("jobs as j","j.contract_id","c.id")
->where('c.id','=',$contract_id)
->where('planner.date','>=',$current_date)
->whereMonth('planner.date',$current_month)
->select(['j.*','ts.*','ps.*','pn.*','c.*','planner.*','ps.name as section_name','ps.id as section_id','planner.date as planner_date','pn.id as planner_note_id'])
->orderBy('planner.date','ASC')
->orderBy('planner.timeslot_id','ASC')
->orderBy('section_id','ASC')
->get();
Above conversation isn't working for me.
It's also not working with ,
->leftJoin("planner_notes as pn","pn.date","planner.date","pn.contract_id","planner.contract_id")
Edit :- No I'm not getting any errors for it. it just considering last On condition.
ON `pn`.`contract_id` = `planner`.`contract_id`
I have one more confusion that if I write two leftjoin then On condition can overwrite or not ?
EDIT-2 :-
It only returns last on condition.
Query from query log is :-
SELECT
`j`.*,
`ts`.*,
`ps`.*,
`pn`.*,
`c`.*,
`planner`.*,
`ps`.`name` AS `section_name`,
`ps`.`id` AS `section_id`,
`planner`.`date` AS `planner_date`,
`pn`.`id` AS `planner_note_id`
FROM
`planner`
INNER JOIN
`timeslots` AS `ts` ON `ts`.`id` = `planner`.`timeslot_id`
INNER JOIN
`planner_sections` AS `ps` ON `ps`.`id` = `planner`.`planner_section_id`
LEFT JOIN
`planner_notes` AS `pn` ON `pn`.`contract_id` = `planner`.`contract_id`
INNER JOIN
`contracts` AS `c` ON `c`.`id` = `planner`.`contract_id`
INNER JOIN
`jobs` AS `j` ON `j`.`contract_id` = `c`.`id`
WHERE
`c`.`id` = 57
AND MONTH(`planner`.`date`) = 6
AND `planner`.`date` >= '2017-06-13'
AND `planner`.`deleted_at` IS NULL
ORDER BY `planner`.`date` ASC

You could do
->leftJoin('planner_notes as pn', function($join){
$join->on('pn.date', '=', 'planner.date');
$join->on('pn.contract_id','=','planner.contract_id');
})
or
change alias of table as
->leftJoin("planner_notes as pn","pn.date",'=',"planner.date")
->leftJoin("planner_notes as pnotes","pnotes.contract_id",'=',"planner.contract_id")

Laravel join with multiple condition
LEFT JOIN
`planner_notes` AS `pn` ON `pn`.`date` = `planner`.`date` and `pn`.`contract_id` = `planner`.`contract_id`
it's equivalent to:
->leftJoin('planner_notes as pn', function($join){
$join->on('pn.date', '=', 'planner.date');
$join->on('pn.contract_id','=','planner.contract_id');
})
laravel.com Ref: method_on
Add an "on" clause to the join.
On clauses can be chained, e.g.
$join->on('contacts.userid', '=', 'users.id') ->on('contacts.infoid', '=', 'info.id')
will produce the following SQL:
on contacts.user_id = users.id and contacts.info_id = info.id

Related

Laravel 6, MYSQL - How to Left Join a Sub Query with GroupBY using Laravel Querybuilder or Model Eloquent?

Im tried this to work it using Laravel Eloquent but i cant get exact query. So i make a raw query to get the data i want. Any one help me how to convert this into laravel eloquent or Query builder?
SELECT users.*,
chat.*
FROM users
LEFT JOIN
(SELECT a.customer_id,
a.time,
b.content
FROM
(SELECT customer_id,
MAX(datetimestamp) TIME
FROM chat_messages
GROUP BY customer_id) a
JOIN chat_messages b ON a.customer_id = b.customer_id
AND a.time = b.datetimestamp) chat ON users.id = chat.customer_id
WHERE users.customer_role != 0
ORDER BY TIME DESC
I think you are trying to get latest chat message for each user , your query can be rewritten using left join to pick the latest record per group and it would be easier to transform such query in laravel's query builder format
SQL
select u.*,c.*
from users u
join chat_messages c on u.id = c.customer_id
left join chat_messages c1 on c.customer_id = c1.customer_id and c.datetimestamp < c1.datetimestamp
where c1.customer_id is null
and u.customer_role != 0
order by c.datetimestamp desc
Query Builder
DB::table('users as u')
->select('u.*, c.*')
->join('chat_messages as c', 'u.id', '=', 'c.customer_id' )
->leftJoin('chat_messages as c1', function ($join) {
$join->on('c.customer_id', '=', 'c1.customer_id')
->whereRaw(DB::raw('c.datetimestamp < c1.datetimestamp'));
})
->whereNull('c1.customer_id')
->where('u.customer_role','!=',0)
->orderBy('c.datetimestamp', 'desc')
->get();
Reference:Laravel Eloquent select all rows with max created_at

I'm not able to convert the Mysql query into laravel query builder

I'm struggling to convert this below MySQL query into laravel query builder. It has a lot of joins and subqueries inside. Can someone please help me?
MYSQL query:
SELECT a.id , b.*, a.agent_id, td.contacts, td.addresses
FROM teams a
left join team_details as td on td.team_id = a.id
LEFT OUTER JOIN
(
SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to
) b ON a.id = b.team_id
order by a.id
I'm trying something like below but got stuck with LEFT OUTER JOIN as it has sub query inside :).
DB::table('teams as a')
->leftjoin('team_details as td', 'td.team_id','=','a.id')
->select('a.id', 'b.*', 'a.agent_id','td.contacts','td.addresses')
->leftouterjoin(DB::select(DB::raw('SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to')))
->get();
Any suggestions, please? I want to apply paginate function paginate() instead of get() for this query builder.
I think you should try.
$cards = DB::select("SELECT a.id , b.*, a.agent_id, td.contacts, td.addresses
FROM teams a
left join team_details as td on td.team_id = a.id
LEFT OUTER JOIN
(
SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to
) b ON a.id = b.team_id
order by a.id");
Hope it help you.

how to use INNER JOIN and IN Clause in same query

i have three tables and want to run INNER JOIN and IN clause on them.
can anyone tell me where i am doing wrong
SELECT `tblinvoices`.id,`tblinvoices`.userid,`firstname`,`lastname`
FROM `tblinvoices`
WHERE `paymentmethod`IN
(SELECT `gateway` FROM `tblpaymentgateways` WHERE `setting`='type' AND `value` = 'CC')
INNER JOIN `tblclients` ON `tblinvoices`.userid=`tblclients`.id"
JOIN comes before WHERE:
SELECT tblinvoices.id,
tblinvoices.userid,
firstname,
lastname
FROM
tblinvoices
INNER JOIN tblclients
ON tblinvoices.userid = tblclients.id
WHERE
paymentmethod IN
(select gateway
FROM tblpaymentgateways
WHERE setting='type'
AND value = 'CC')

How to get multidimensional array from yii 'CDbDataReader::readAll'

It is two tables in a database. The first tablse contains a list of products, and the second contains a list of options. For one product may be several options. I have built next query with yii query bulder
SELECT DISTINCT `p`.`id`,
`p`.`name`,
`pc`.`category_id`,
`c`.`name` AS `category_name`,
`ov`.`value_str` AS `option_value`
FROM `cms_product` `p`
LEFT JOIN `cms_product_category` `pc` ON p.id = pc.product_id
LEFT JOIN `cms_category` `c` ON c.id = pc.category_id
LEFT JOIN `cms_product_option_set` `pos` ON p.id = pos.product_id
LEFT JOIN `cms_option_variant` `ov` ON ov.id=pos.option_variant_id
ORDER BY `p`.`id` ASC LIMIT 100
But yii function 'CDbDataReader::readAll()' returns only one option for each product. If I execute this query in MySQL it works good. How I can get right result with yii
Use queryAll() method like below:
$sql="SELECT DISTINCT `p`.`id`,
`p`.`name`,
`pc`.`category_id`,
`c`.`name` AS `category_name`,
`ov`.`value_str` AS `option_value`
FROM `cms_product` `p`
LEFT JOIN `cms_product_category` `pc` ON p.id = pc.product_id
LEFT JOIN `cms_category` `c` ON c.id = pc.category_id
LEFT JOIN `cms_product_option_set` `pos` ON p.id = pos.product_id
LEFT JOIN `cms_option_variant` `ov` ON ov.id=pos.option_variant_id
ORDER BY `p`.`id` ASC LIMIT 100";
Then:
$result=Yii::app()->db->createCommand($sql)->queryAll();
This will return you an array with the result.

How To Left Join A Table Based On The Value Received From Inner Join Of The Same Query

I am trying to run the following query, and I keep getting a syntax error. The query works fine without the LEFT JOIN. How can I implement the LEFT JOIN without error?
SELECT
m.mid,
m.seq,
m.created_on,
m.created_by,
m.body,
r.status,
u.username_clean
FROM message_recipient r
INNER JOIN message m
ON m.mid = r.mid AND m.seq = r.seq
WHERE r.uid = ".$logged_in_id."
AND r.status in ('A', 'N')
AND r.seq = (
SELECT
MAX(rr.seq)
FROM message_recipient rr
WHERE rr.mid = m.mid
AND rr.status in ('A', 'N')
)
AND IF (m.seq=1 and m.created_by = ".$logged_in_id." , 1=0, 1=1)
ORDER BY created_on DESC
LEFT JOIN users u
ON u.user_id = m.created_by
The LEFT JOIN is in the wrong place. All of the JOINs need to be before the WHERE clause:
SELECT
m.mid,
m.seq,
m.created_on,
m.created_by,
m.body,
r.status,
u.username_clean
FROM message_recipient r
INNER JOIN message m
ON m.mid = r.mid AND m.seq = r.seq
LEFT JOIN users u
ON u.user_id = m.created_by
WHERE r.uid = ".$logged_in_id."
AND r.status in ('A', 'N')
AND r.seq = (
SELECT
MAX(rr.seq)
FROM message_recipient rr
WHERE rr.mid = m.mid
AND rr.status in ('A', 'N')
)
AND IF (m.seq=1 and m.created_by = ".$logged_in_id." , 1=0, 1=1)
ORDER BY created_on DESC
The SQL clauses go in the following order:
SELECT
FROM
JOIN
WHERE
GROUP BY
ORDER BY
Even if you have multiple joins they will all appear before the WHERE

Categories