如何获取特定状态或 ID 的总和并在 laravel 中加入 3 table
How to get sum of specific status or id and joining 3 table in laravel
my table
table1
|table1_id | name |
| 1 john |
| 2 dave |
| 3 carl |
table2
|table2_id| table1_id| type |status
| 1 | 1 | shoes |paid
| 2 | 1 | bag |paid
| 3 | 2 | bag |paid
| 4 | 2 | shoes |unpaid
table3
|table3_id|table2_id|item |amount|
|1 | 1 |nike |1000 |
|2 | 1 |adidas |2000 |
|3 | 2 |lv |1000 |
|4 | 3 |lv1 |2000 |
|5 | 3 |lv |1000 |
|6 | 4 |adidas |1000 |
this is the result I want to display
john --- total paid shoes and bag dave <-- total of bag,'shoes is
unpaid so 1000 is not added to total'
|name|total |
|john|4000 |
|dave|3000 |
|carl|0 |
this is my controller it gives me an error the total is same in all name
public function index()
{
$fetch = DB::table('table1')
->leftjoin('table2','table1.table1_id','=','table2.table1_id')
->leftjoin('table3','table2.table2_id','=','table3.table2_id')
->select('table1.*','table2.*',DB::raw('(select sum(table3.amount) from table3
join table2 on table2.table2_id = table3.table2_id
where table.status = "paid") as used'))
->groupBy('table1.table1_id')
->get();
return $fetch;
}
您可以考虑加入子查询,这当然是一种方法。这是一个例子:
$sales = DB::table('table3')
->select('table2_id', DB::raw('sum(amount) as cat_sale_amount'))
->groupBy('table2_id');
$table2Sales = DB::table('table2')
->select('table1_id', DB::raw('sum(cat_sale_amount) as total_sale_amount'))
->joinSub($sales, 'sales', function($join){
$join->on('table2.id', '=', 'sales.table2_id');
})
->where('status', 'paid')
->groupBy('table1_id');
$userTotalSales = DB::table('table1')
->select('name', 'total_sale_amount as total')
->leftJoinSub($table2Sales, 'table2Sales', function($join){
$join->on('table1.id', '=', 'table2Sales.table1_id');
})->get();
第一个子查询将根据 table1 中的 type
给出总销售额,方法是对 table2_id 进行分组并对金额使用求和运算。因此结果将列出 user_id/table2_id 和每种类型的数量。
第二个子查询将通过加入第一个子查询给出个人 customers/user 的总销售额。按 cutomer/user/table1_id 分组以获得第一个查询的金额总和,以获得单个客户的总数。
将第二个加入用户table,你应该会得到想要的结果。
my table
table1
|table1_id | name |
| 1 john |
| 2 dave |
| 3 carl |
table2
|table2_id| table1_id| type |status
| 1 | 1 | shoes |paid
| 2 | 1 | bag |paid
| 3 | 2 | bag |paid
| 4 | 2 | shoes |unpaid
table3
|table3_id|table2_id|item |amount|
|1 | 1 |nike |1000 |
|2 | 1 |adidas |2000 |
|3 | 2 |lv |1000 |
|4 | 3 |lv1 |2000 |
|5 | 3 |lv |1000 |
|6 | 4 |adidas |1000 |
this is the result I want to display john --- total paid shoes and bag dave <-- total of bag,'shoes is unpaid so 1000 is not added to total'
|name|total |
|john|4000 |
|dave|3000 |
|carl|0 |
this is my controller it gives me an error the total is same in all name
public function index()
{
$fetch = DB::table('table1')
->leftjoin('table2','table1.table1_id','=','table2.table1_id')
->leftjoin('table3','table2.table2_id','=','table3.table2_id')
->select('table1.*','table2.*',DB::raw('(select sum(table3.amount) from table3
join table2 on table2.table2_id = table3.table2_id
where table.status = "paid") as used'))
->groupBy('table1.table1_id')
->get();
return $fetch;
}
您可以考虑加入子查询,这当然是一种方法。这是一个例子:
$sales = DB::table('table3')
->select('table2_id', DB::raw('sum(amount) as cat_sale_amount'))
->groupBy('table2_id');
$table2Sales = DB::table('table2')
->select('table1_id', DB::raw('sum(cat_sale_amount) as total_sale_amount'))
->joinSub($sales, 'sales', function($join){
$join->on('table2.id', '=', 'sales.table2_id');
})
->where('status', 'paid')
->groupBy('table1_id');
$userTotalSales = DB::table('table1')
->select('name', 'total_sale_amount as total')
->leftJoinSub($table2Sales, 'table2Sales', function($join){
$join->on('table1.id', '=', 'table2Sales.table1_id');
})->get();
第一个子查询将根据 table1 中的 type
给出总销售额,方法是对 table2_id 进行分组并对金额使用求和运算。因此结果将列出 user_id/table2_id 和每种类型的数量。
第二个子查询将通过加入第一个子查询给出个人 customers/user 的总销售额。按 cutomer/user/table1_id 分组以获得第一个查询的金额总和,以获得单个客户的总数。
将第二个加入用户table,你应该会得到想要的结果。