我想加入两个包含两列的表, 它在phpmyadmin工作。但它没有与laravel合作。 那么如何在laravel结构中转换它。
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
但是当我把它放在laravel查询结构中时,它无法正常工作。
$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();
以上的谈话对我不起作用。
它也没有合作,
->leftJoin("planner_notes as pn","pn.date","planner.date","pn.contract_id","planner.contract_id")
修改: - 不,我没有收到任何错误。它只是考虑到最后的条件。
ON `pn`.`contract_id` = `planner`.`contract_id`
我还有一个困惑,如果我写两个leftjoin然后On条件可以覆盖或不覆盖?
EDIT-2: - 它只返回最后一个条件。 查询日志查询是: -
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
答案 0 :(得分:1)
Laravel加入多个条件
LEFT JOIN
`planner_notes` AS `pn` ON `pn`.`date` = `planner`.`date` and `pn`.`contract_id` = `planner`.`contract_id`
它相当于:
->leftJoin('planner_notes as pn', function($join){
$join->on('pn.date', '=', 'planner.date');
$join->on('pn.contract_id','=','planner.contract_id');
})
laravel.com参考:method_on
在联接中添加“on”子句。
On子句可以链接,例如
$ join-> on('contacts.userid','=','users.id') - > on('contacts.infoid','=','info.id')
将生成以下SQL:
on contacts.user_id = users.id和contacts.info_id = info.id
答案 1 :(得分:0)
你可以做到
->leftJoin('planner_notes as pn', function($join){
$join->on('pn.date', '=', 'planner.date');
$join->on('pn.contract_id','=','planner.contract_id');
})
或
将表的别名更改为
->leftJoin("planner_notes as pn","pn.date",'=',"planner.date")
->leftJoin("planner_notes as pnotes","pnotes.contract_id",'=',"planner.contract_id")