dsg56465 2016-06-12 16:39
浏览 231

Laravel查询构建器SUM返回相乘的值

I have the following query and been fighting with from yesterday after solving previous issue with the help of great stack-overflowers yesterday's question.

$sales = DB::table('sales')
        ->leftJoin('store_configs', 'store_configs.id', '=', 'sales.store_config_id')
        ->leftJoin('category_sales', 'category_sales.sale_id', '=', 'sales.id')
        ->leftJoin('categories', 'categories.id', '=', 'category_sales.category_id')
        ->leftJoin('departments', 'departments.category_id', '=', 'categories.id')
        ->leftJoin('department_sales', 'department_sales.sale_id', '=', 'sales.id')
        ->select('sales.date',

            DB::raw('sales.id as sales_id'),
            DB::raw('(IFNULL(sum(department_sales.amount), 0)) as department_sales'),
            DB::raw('(IFNULL(sum(category_sales.amount), 0)) as cat_total')

        )

        ->groupBy('date', 'sales.id')->orderBy('date', 'desc')->get();

The following is the result output from above query and it is wrong:

enter image description here

I have a table structure with following data: categories_sales has following data: enter image description here

department_sales

enter image description here

As per the table : I expected result should have been :

department_sales: 10 for sale_id = 1
department_sales: 5 for sale_id = 2

category_sales: 200 for sale_id 1
category_sales: 30 for sale_id 2

Can someone please give me some idea ? I will be really really thankful.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类