繁体   English   中英

将不同的 select 和查询与同一张表的不同 where 相结合(Laravel 方式)

[英]Combining different select sum queries with different where from same table (Laravel way)

我从同一个表中读取数据的 select 查询略有不同。

select user_id
     , SUM(credit_movement) as testing_fees
  from transactions
 where `type` in ('Testing', 'Testing Data') 
   and `user_id` in (118,124,352 ...)
group by `user_id`
select user_id, SUM(credit_movement) as production_fees 
from `transactions` 
where `type` in ('Production', 'Production Data') and `user_id` in (152,521,1341, ...)
group by `user_id`

typeuser_id正在改变。

在 Laravel 中,我最终为每种类型使用了 1 个查询,但有 10 种类型,这意味着 10 个数据库连接 - 这不好。

$values['testing_fees'] = \DB::table("transactions")
     ->select(\DB::raw("user_id, SUM(credit_movement) as testing_fees"))
     ->whereIn('type', ["Testing", "Testing Data"])
     ->whereIn('user_id', $userIdsToBeUsed)
     ->whereDate('created_at', '>=', $fromDate->toDateString())
     ->whereDate('created_at', '<=', $toDate->toDateString())
     ->groupBy('user_id')
     ->get();

$values['production_fees'] = \DB::table("transactions")
     ->select(\DB::raw("user_id, SUM(credit_movement) as production_fees"))
     ->whereIn('type', ["Production", "Production Data"])
     ->whereIn('user_id', $userIdsToBeUsed)
     ->whereDate('created_at', '>=', $fromDate->toDateString()) // this is common
     ->whereDate('created_at', '<=', $toDate->toDateString())   // this is common
     ->groupBy('user_id')                                       // this is common
     ->get();

将这些查询组合到一个查询中以便我建立 1 个数据库连接以获取所有数据的好方法是什么?

(我正在寻找实现此目的的 Laravel Query Builder 方式)

您可以使用 UNION 关键字: https://www.w3schools.com/sql/sql_ref_union.asp

所以组合查询将是这样的:

select user_id
     , SUM(credit_movement) as testing_fees
  from transactions
 where `type` in ('Testing', 'Testing Data') 
   and `user_id` in (118,124,352 ...)
group by `user_id`
UNION
select user_id, SUM(credit_movement) as production_fees 
from `transactions` 
where `type` in ('Production', 'Production Data') and `user_id` in (152,521,1341, ...)
group by `user_id`

如果您需要允许重复,请使用UNION ALL而不是UNION

如果您想避免混淆哪个是哪个,您可以使用第三个键来告诉您正在处理的数据类型。 查看Laravel docs ,您的代码应如下所示:

<?php

$values['testing_fees'] = \DB::table("transactions")
     ->select(\DB::raw("user_id, SUM(credit_movement) as testing_fees,'testing as type'"))
     ->whereIn('type', ["Testing", "Testing Data"])
     ->whereIn('user_id', $userIdsToBeUsed)
     ->whereDate('created_at', '>=', $fromDate->toDateString())
     ->whereDate('created_at', '<=', $toDate->toDateString())
     ->groupBy('user_id');


$values['production_fees'] = \DB::table("transactions")
     ->select(\DB::raw("user_id, SUM(credit_movement) as production_fees,'production' as type"))
     ->whereIn('type', ["Production", "Production Data"])
     ->whereIn('user_id', $userIdsToBeUsed)
     ->whereDate('created_at', '>=', $fromDate->toDateString()) // this is common
     ->whereDate('created_at', '<=', $toDate->toDateString())   // this is common
     ->groupBy('user_id');                                      // this is common


// and so on for $values

$query = array_shift($values);


foreach($values as $key => $sub_query){
    $query->union($sub_query);
}


$data = $query->get();

dd($data);

注意: ->get()仅在我们对所有子查询进行union后才适用。

暂无
暂无

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

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