如何编写通过连接不同表的Eloquent查询

huangapple go评论60阅读模式
英文:

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";
}

huangapple
  • 本文由 发表于 2023年3月31日 18:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定