【问题标题】:Remove duplicated data based on quintity根据数量删除重复数据
【发布时间】:2021-04-08 11:58:43
【问题描述】:

想根据数量去除重复数据,

DB::table(DB::raw('tenant_db.sales_data as b'))
            ->selectRaw('a.product_id as product_id, b.product_id as bought_with, count(*) as times_bought_together, SUM(a.qty) AS aQuantity, SUM(b.qty) AS bQuantity')
            ->join(DB::raw('tenant_db.sales_data as a'), function ($join){
                $join->on('a.sale_id', '=', 'b.sale_id');
                $join->on('a.product_id', '<>', 'b.product_id');
            })
            ->groupBy('a.product_id', 'b.product_id')
            ->orderBy('times_bought_together', 'DESC')
            ->get();

类似的东西

            ->having('aQuantity', '>', 'bQuantity')

我得到的结果


    0 => {#741 ▼
      +"product_id": 41
      +"bought_with": 42
      +"times_bought_together": 3
      +"aQuantity": "4"
      +"bQuantity": "3"
    }
    1 => {#743 ▼
      +"product_id": 42
      +"bought_with": 41
      +"times_bought_together": 3
      +"aQuantity": "3"
      +"bQuantity": "4"

我只想在 aQuantity 高于 bQuantity 时查看结果,但我需要在查询中使用它,否则它将无法与来自 yarja 的数据表一起使用

【问题讨论】:

  • ->leftJoin(DB::raw('tenant_db.sales_data as a'), function ($join){ ..... 像这样使用左连接

标签: database laravel eloquent


【解决方案1】:

你可以使用Have Raw

->havingRaw('SUM(a.qty) > SUM(b.qty)')

【讨论】:

    猜你喜欢
    • 2017-02-12
    • 1970-01-01
    • 2016-09-16
    • 2021-05-14
    • 2016-05-16
    • 2018-10-25
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多