gpt4 book ai didi

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

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

我正在对两个查询进行联合,我想向结果添加一个 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')

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

最佳答案

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

$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();

关于php - Laravel 将 where 子句添加到联合结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37408758/

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