我正在尝试获取calc.transport的总数 表3_product_folder包含重复值(列'folder_id'),如何防止SUM搞乱? 我没有尝试过DISTINCT。
有什么建议吗?
SELECT
SUM(calc.transport) AS transport
FROM 0_calculatie_id AS calc
LEFT JOIN 0_calculatie_inh_id AS id
ON id.calculatie_id = calc.id
LEFT JOIN 3_product_folder AS fld_id
ON fld_id.product_id = id.id
LEFT JOIN 4_folder_dossier AS fld
ON fld.product_folder_id = fld_id.folder_id
LEFT JOIN 10_factuur_inh AS fac_inh
ON fac_inh.folder_id = fld.id
LEFT JOIN 9_factuur_id AS fac
ON fac.id = fac_inh.factuur_id
WHERE YEAR(fac.datum_verstuurd) = ".$row['jaar']." AND MONTH(fac.datum_verstuurd) = ".$row_mth2['maand']."
我尝试过这个嵌套查询,但并非所有项目都计算在内:
SELECT
SUM(temp.transport) AS transport
FROM
(SELECT DISTINCT
calc.transport,
fld_id.folder_id
FROM 0_calculatie_id AS calc
LEFT JOIN 0_calculatie_inh_id AS id
ON id.calculatie_id = calc.id
LEFT JOIN 3_product_folder AS fld_id
ON fld_id.product_id = id.id
LEFT JOIN 4_folder_dossier AS fld
ON fld.product_folder_id = fld_id.folder_id
LEFT JOIN 10_factuur_inh AS fac_inh
ON fac_inh.folder_id = fld.id
LEFT JOIN 9_factuur_id AS fac
ON fac.id = fac_inh.factuur_id
WHERE YEAR(fac.datum_verstuurd) = 2012 AND MONTH(fac.datum_verstuurd) = 7) AS temp
答案 0 :(得分:1)
表的结构是什么?
您尚未对SQL语句进行分组:
WHERE YEAR(fac.datum_verstuurd) = ".$row['jaar']." AND MONTH(fac.datum_verstuurd) = ".$row_mth2['maand']."
GROUP BY fld_id.product_id