【发布时间】: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){ ..... 像这样使用左连接