英文:
How to write eloquent query by joining different tables
问题
我有4个表。
1)materials
2)advertisements
3)offerrequests
4)transactions
我需要编写一个查询,该查询将选择涉及交易的每种材料的重量总和。
我尝试的查询如下,
Transaction::->select(DB::raw("IF(transactions.is_sell = '1', advertisements.weight, offerrequests.weight) as weight"),DB::raw("IF(transactions.is_sell = '1', advertisements.material_id, offerrequests.material_id) as material_id"))
->leftJoin('advertisements', function ($join) {
$join->on('advertisements.id', '=', 'transactions.post_id');
})
->leftJoin('offerrequests', function ($join) {
$join->on('offerrequests.id', '=', 'transactions.post_id');
})
->leftJoin('materials', function ($join) {
$join->on('materials.id', '=', 'material_id');
})
->groupBy('material_id')->get();
交易表
id | material_id | is_sell | post_id |
---|---|---|---|
1 | 1 | 1 | 20 |
广告表
id | material_id | weight |
---|---|---|
1 | 1 | 10 |
求购请求表
id | material_id | weight |
---|---|---|
1 | 1 | 20 |
材料表
id | name |
---|---|
1 | 塑料 |
2 | 纸 |
我的预期结果将如下所示
weight | material |
---|---|
30 | 塑料 |
50 | 纸 |
英文:
I have 4 tables.
1)materials
2)advertisements
3)offerrequests
4)transactions
I need to write a query that will select the sum of the weight of transactions per material involved in the transaction
The query I tried is as follows,
Transaction::->select(DB::raw("IF(transactions.is_sell = '1', advertisements.weight, offerrequests.weight) as weight"),DB::raw("IF(transactions.is_sell = '1', advertisements.material_id, offerrequests.material_id) as material_id"))
->leftJoin('advertisements', function ($join) {
$join->on('advertisements.id', '=', 'transactions.post_id');
})
->leftJoin('offerrequests', function ($join) {
$join->on('offerrequests.id', '=', 'transactions.post_id');
})
->leftJoin('materials', function ($join) {
$join->on('materials.id', '=', 'material_id');
})
->groupBy('material_id')->get();
Transactions table
id | material_id | is_sell | post_id |
---|---|---|---|
1 | 1 | 1 | 20 |
Advertisements table
id | material_id | weight |
---|---|---|
1 | 1 | 10 |
Offerrequests table
id | material_id | weight |
---|---|---|
1 | 1 | 20 |
Materials table
id | name |
---|---|
1 | plastic |
2 | paper |
My expected result will be like this
weight | material |
---|---|
30 | Plastic |
50 | paper |
答案1
得分: 0
你可以尝试以下查询:
$results = DB::table('transactions as t')
->leftJoin('advertisements as a', function ($join) {
$join->on('a.id', '=', 't.post_id')
->where('t.is_sell', '=', 1);
})
->leftJoin('offerrequests as o', function ($join) {
$join->on('o.id', '=', 't.post_id')
->where('t.is_sell', '=', 0);
})
->join('materials as m', function ($join) {
$join->on('m.id', '=', DB::raw('COALESCE(a.material_id, o.material_id)'));
})
->groupBy('m.id')
->selectRaw('SUM(CASE WHEN t.is_sell = 1 THEN a.weight ELSE o.weight END) AS weight')
->select('m.name AS material')
->get();
foreach ($results as $result) {
echo $result->weight . "\t" . $result->material . "\n";
}
英文:
You can try the query like:
$results = DB::table('transactions as t')
->leftJoin('advertisements as a', function ($join) {
$join->on('a.id', '=', 't.post_id')
->where('t.is_sell', '=', 1);
})
->leftJoin('offerrequests as o', function ($join) {
$join->on('o.id', '=', 't.post_id')
->where('t.is_sell', '=', 0);
})
->join('materials as m', function ($join) {
$join->on('m.id', '=', DB::raw('COALESCE(a.material_id, o.material_id)'));
})
->groupBy('m.id')
->selectRaw('SUM(CASE WHEN t.is_sell = 1 THEN a.weight ELSE o.weight END) AS weight')
->select('m.name AS material')
->get();
and print the weight and material name as:
foreach ($results as $result) {
echo $result->weight . "\t" . $result->material . "\n";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论