【问题标题】:Laravel Left join add where clause to right tableLaravel Left join 将 where 子句添加到右表
【发布时间】:2016-05-30 06:36:50
【问题描述】:

如果我将 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') ;

【问题讨论】:

  • leftJoin之前添加where子句

标签: mysql laravel eloquent


【解决方案1】:

在左连接中,所有将作用于右表的 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') ;
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-06
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    相关资源
    最近更新 更多