繁体   English   中英

Laravel Left join将where子句添加到右表

[英]Laravel Left join add where clause to right table

如果我向左连接添加一个where子句,那么这个where子句在右表上工作,我只得到那些可以匹配右表的结果。

$notifications = \DB::table('notifications')
            ->select(\DB::raw("notifications.uuid ,images.local_path as title_image" ))
            ->leftJoin('images','images.owner_uuid', '=' ,'notifications.uuid')
where('images.relation','=','notification_title') ;

如何将此where子句添加到左连接,这不会产生此问题?

where('images.relation','=','notification_title') ;

在左连接中,所有要在右表上执行的where子句必须添加到JOIN语句本身。 使用此代码

$notifications = \DB::table('notifications')
            ->select(\DB::raw("notifications.uuid , images.local_path as title_image" ))
            ->leftJoin('images',function ($join) {
                $join->on('images.owner_uuid', '=' , 'notifications.uuid') ;
                $join->where('images.relation','=','notification_title') ;
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM