Laravel 将 where 子句添加到联合结果

问题描述 投票:0回答:6

我正在对两个查询进行联合,并且我想向结果添加一个 where 子句,但 where 子句仅添加到第一个查询。我该如何解决这个问题?

    $notifications = DB::table('notifications')
        ->select(DB::raw("notifications.uuid ,notifications.brand_id" ))
    $posts = DB::table('posts')
        ->select(DB::raw("posts.uuid ,posts.brand_id" ))
        ->unionAll ($notifications)
        ->orderBy('created_at' , 'desc')
        ->where('brand_ids' , '=' , '2')
    $result  = $posts->get();

我想要这条线

           ->where('brand_id' , '=' , '2')

要添加到整个联合,但仅添加到其中一个查询。

php mysql laravel laravel-5 union
6个回答
0
投票

我不确定是否有更好的方法,但这对我有用:

$notifications = DB::table('notifications')
                ->select(DB::raw("notifications.uuid ,notifications.brand_id"));
$posts = DB::table('posts')
        ->select(DB::raw("posts.uuid ,posts.brand_id"))
        ->unionAll($notifications)
        ->orderBy('created_at' , 'desc');
$result = DB::table(DB::raw("({$posts->toSql()}) as posts"))
                ->mergeBindings($posts)
                ->where('brand_id', '2')
                ->get();

0
投票

这取决于 MYSQL 而不是 Laravel。

从这里的示例中可以看到,如果您想在 union 之后执行 where 操作,则需要将其作为子查询来完成。 SQL 中 UNION 之后的 WHERE 语句?

希望这个解决方案可以帮助任何人。

问候


0
投票

首先,您的查询无效,因为您按

created_at
对结果进行排序,并且没有选择此列。我希望我的代码能帮助你

$notifications = DB::table('notifications')
    ->select(DB::raw("uuid, brand_id, created_at"))
    ->where('brand_id', 2);

DB::table('posts')
    ->select(DB::raw("uuid, brand_id, created_at"))
    ->unionAll($notifications)
    ->orderBy('created_at', 'DESC')
    ->where('brand_id', 2)
    ->dd();

0
投票

在 Laravel 中,where 子句仅应用于查询构建器链的最后一段。如果你想对整个联合结果应用where条件,你可以使用子查询。

$notifications = DB::table('notifications')
    ->select(DB::raw("notifications.uuid, notifications.brand_id"));

$posts = DB::table('posts')
    ->select(DB::raw("posts.uuid, posts.brand_id"))
    ->unionAll($notifications);

$result = DB::table(DB::raw("({$posts->toSql()}) as sub"))
    ->mergeBindings($posts)
    ->orderBy('created_at', 'desc')
    ->where('brand_id', '=', '2')
    ->get();


0
投票

要向整个联合添加 where 子句而不只是一个查询,您需要使用子查询。以下是如何修改代码的示例:

$notifications = DB::table('notifications')
    ->select(DB::raw("notifications.uuid, notifications.brand_id"));

$posts = DB::table('posts')
    ->select(DB::raw("posts.uuid, posts.brand_id"))
    ->unionAll($notifications);

$result = DB::table(DB::raw("({$posts->toSql()}) as combined"))
    ->mergeBindings($posts) // This is important to merge the bindings
    ->orderBy('created_at', 'desc')
    ->where('brand_id', '=', '2')
    ->get();

我通过使用 DB::raw("({$posts->toSql()}) 作为组合") 和 mergeBindings($posts) 来使用子查询,以确保绑定正确合并。然后,where 子句将应用于整个结果集。


0
投票

要将 where 子句“->where('brand_id' , '=' , '2')”应用于 unionAll 之后的两个查询,您可以将整个联合操作包含在子查询中。然后,您可以将 where 子句应用于外部查询。具体方法如下:

    $notifications = DB::table('notifications')
        ->select(DB::raw("notifications.uuid, notifications.brand_id"));
    
    $posts = DB::table('posts')
        ->select(DB::raw("posts.uuid, posts.brand_id"));
    
    $result = DB::table(DB::raw("({$posts->toSql()} UNION ALL {$notifications->toSql()}) as union_result"))
        ->mergeBindings($posts)
        ->mergeBindings($notifications)
        ->orderBy('created_at', 'desc')
        ->where('brand_id', '=', '2')
        ->get();
© www.soinside.com 2019 - 2024. All rights reserved.