Multiple Left Join with sum

后端 未结 2 761
深忆病人
深忆病人 2021-01-16 07:35

I\'m trying to display in a table (with data tables plugin) informations with sum from 3 tables using Left Join in sql query. I succeeded to edit server-side query and disp

2条回答
  •  孤城傲影
    2021-01-16 08:12

    That is the final and seems to work.

    $year=date('Y');
    $sQuery = "SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns)).", 
    t2.total_changes,
    t1.operation_BP+IFNULL(total_changes,0) AS operation_total, 
    t3.total_conso
    FROM $sTable AS t1
    LEFT JOIN (SELECT tt2.change_year_operation, IFNULL(SUM(tt2.change_amount),0) AS total_changes FROM wp_dri_budget_changes tt2 GROUP BY tt2.change_year_operation) t2 ON t1.operation_year_number = t2.change_year_operation
    LEFT JOIN (SELECT tt3.expense_year_operation, IFNULL(SUM(tt3.expense_enga_amount),0) AS total_conso FROM wp_dri_budget_expenses tt3 GROUP BY tt3.expense_year_operation) AS t3 ON t1.operation_year_number = t3.expense_year_operation
    WHERE t1.operation_year=".$year." AND t1.operation_active=1 $sWhere $sOrder $sLimit";
    

    OPERATION_NUMBER NAME BP CHANGES TOTAL TOTAL_CONSO


    15P731OV001 project1 28000 (null) 28000 (null)

    13P0012OV001 project2 612500 -60000 552500 21000

    **But now IFNULL(x,0) seems not working anymore and null (NaN.N in datatables) are display when sum results are NULL **

提交回复
热议问题