如何在laravel中对多个子查询求和

问题描述

我有3个表,分别称为pencairan,induk_pencairan,turunan_belanja。现在我想在另一个表中显示total_order,但它的相关表是这样的:

pencairan

pencairan
+----+------------+------------+------------+
| id |  induk_id  | price      |   qty   
+----+------------+------------+------------+
|  1 |   1        |   1000     |    10   //  so from this i can create total 10000 from (price*qty)
+----+------------+------------+------------+
|  2 |   1        |   2000     |    5    // and this 10000
+----+------------+------------+------------+
|  3 |   2        |   5000     |    10   // and this 50000
+----+------------+------------+------------+
|  4 |   2        |   1000     |    10   // and this 10000
+----+------------+------------+------------+

induk_pencairan:

induk_pencairan
+----+------------+------------+------------+
| id |turunan_id  | some_data  |    some_data
+----+------------+------------+------------+
|  1 |   1        | some_data  |        -> this total is 20000 from pencairan table
+----+------------+------------+------------+
|  2 |   1        | some_data  |        -> total = 60000
+----+------------+------------+------------+
|  3 |   2        | some_data  |        -> total ....
+----+------------+------------+------------+
|  4 |   2        | some_data  |        -> total ....
+----+------------+------------+------------+

turunan_belanja:

turunan_belanja
+----+------------+------------+------------+
| id |    name    | balance    |  some data . . .
+----+------------+------------+------------+
|  1 |   phone    | 10000000   |      -> in here i want to show 80000 its total from induk_pencairan
+----+------------+------------+------------+
|  2 |     PC     | 50000000   |      -> . . . . .
+----+------------+------------+------------+
|  3 | Electronic | 500000000  |      -> . . .  .
+----+------------+------------+------------+

这3个表是相关的,现在我想显示这样的数据:

turunan_belanja
+----+------------+------------+------------+
| id |  name      |  balance   |   total_order (its from table induk_pencairan)
+----+------------+------------+------------+
|  1 |   phone    | 10000000   |    80000
+----+------------+------------+------------+
|  2 |     PC     | 50000000   |    ......
+----+------------+------------+------------+
|  3 | Electronic | 50000000   |    . . .  .
+----+------------+------------+------------+

及其与我的关系

 Models IndukPencairan
    public function induk_pencairan()
    {
        return $this->belongsTo(\App\Models\IndukPencairan::class,'turunan_id');
    }

 Models Pencairan
    public function pencairan()
    {
        return $this->belongsTo(\App\Models\Order::class,'induk_id');
    }

那么我能做什么呢?

现在我的步骤就是像这样在category_order上进行选择:

 public function get_belanja()
    {
        $belanja = TurunanBelanja::where('status','active')->get();
        return response()->json($belanja,200);
    }

UPDATE,我像这样创建此子查询,但是在第二次查询时出现错误

     $belanja = DB::table("induk_pencairan")
    ->select("induk_pencairan.*",DB::raw("(SELECT SUM(pencairan.harga*qty) FROM pencairan
                          WHERE pencairan.indukpencairan_id = induk_pencairan.id
                          GROUP BY pencairan.indukpencairan_id) as product_stock"))
    ->get();

    $belanja2 = DB::table("turunan_belanja")
    ->select("turunan_belanja.*",DB::raw("(SELECT SUM(product_stock) FROM induk_pencairan
                          WHERE induk_pencairan.rek_id = turunan_belanja.id
                          GROUP BY induk_pencairan.rek_id) as total"))
    ->get();

错误是未定义的列product_stock -如何从查询1到查询2解析此product_stock?

解决方法

在纯SQL中,您可以通过以下方式在单个查询中获取汇总数据

select t.id,t.name,t.balance,coalesce(sum(p.price * p.qty),0) total_order
from turunan_belanja t
left join induk_pencairan i on t.id  =i.turunan_id
left join pencairan p on i.id = p.induk_id
group by t.id,t.balance

DEMO

您可以将上述查询转换为laravel查询构建器,

$results = DB::table('turunan_belanja AS t')
    ->select([
    't.id','t.name','t.balance'
    DB::raw('coalesce(sum(p.price * p.qty),0) AS total_order')
    ])
    ->leftJoin('induk_pencairan AS i','i.turunan_id','=','t.id' )
    ->leftJoin('pencairan AS p','p.induk_id','i.id')
    ->groupBy('t.id')
    ->groupBy('t.name')
    ->groupBy('t.balance')
    ->get();

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...