gpt4 book ai didi

mysql - Laravel Left join 向右表添加 where 子句

转载 作者:可可西里 更新时间:2023-11-01 07:17:44 25 4
gpt4 key购买 nike

如果我在左连接中添加一个 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') ;
});

关于mysql - Laravel Left join 向右表添加 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37519096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com