我的laravel控制器中有3个查询:
$data1=DB::table('tabl1')->where('brandid',$brandid)
->select('id', 'name', DB::raw('sum(amount) AS SUM'))
->get();
$data2=DB::table('tabl2')->where('brandid',$brandid)
->select('id', 'name', DB::raw('sum(tax) AS SUM'))
->get();
$data3=DB::table('tabl3')->where('brandid',$brandid)
->select('id', 'name', DB::raw('sum(tax2) AS SUM'))
->get();
我想在3个表之间使用union选择id,name和3 SUM的总和。我放弃了,任何想法?
答案 0 :(得分:1)
你可以这样做
$data1=DB::table('tabl1')->where('brandid',$brandid)
->select('id', 'name', DB::raw('sum(amount) AS SUM'));
$data2=DB::table('tabl2')->where('brandid',$brandid)
->select('id', 'name', DB::raw('sum(tax) AS SUM'));
$data3=DB::table('tabl3')->where('brandid',$brandid)
->select('id', 'name', DB::raw('sum(tax2) AS SUM'));
$data = $data1->unionAll($data2)->unionAll($data3);
$data = $data->get();
没有测试过,但我认为它应该可行