编辑:澄清一下,我想要获得所有节点,无论它们是否有图像。此外,如果图像中没有相应的记录,我不想从Images_Nodes返回任何记录。这是一个更复杂的查询的简化版本,所以不要想我告诉我我的数据库设计不正确。
我正在使用Laravel 5.5并希望进行稍微复杂的查询。
select
n.Node_id, i.Image_id
from
Nodes AS n
left join Nodes_Images AS n_i
join Images i ON n_i.Image_id = i.Image_id
ON n.Node_id = n_i.Node_id
我以为
DB::table('Nodes AS n')
->leftJoin('Nodes_Images AS n_i', function ($join) {
$join->on('n.node_id', '=', 'n_i.node_id')
->join('images AS i', 'n_i.image_id', '=', 'i.image_id');
})->select('n.node_id', 'i.image_id');
会产生它,但它会返回
select
[n].[node_id], [i].[image_id]
from
[Nodes] as [n]
left join [Nodes_Images] as [n_i]
on [n].[node_id] = [n_i].[node_id]
和这个
DB::table('Nodes AS n')
->leftJoin('Nodes_Images AS n_i', 'n.node_id', '=', 'n_i.node_id')
->join('images AS i', 'n_i.image_id', '=', 'i.image_id')
->select('n.node_id', 'i.image_id');
生成这个,它不会将图像连接嵌套在外连接中
select
[n].[node_id], [i].[image_id]
from
[Nodes] as [n]
left join [Nodes_Images] as [n_i] on [n].[node_id] = [n_i].[node_id]
inner join [images] as [i] on [n_i].[image_id] = [i].[image_id]
答案 0 :(得分:0)
这使它成为Laravel 5.6.1