这是我的SQL代码。我选择了每行的价格,每行的里程碑和ID以加入表格,但是我只有一行的结果。

PHP代码做多个过滤器:

if($i==0)
{
    if($fc == "projet")
    {
        $filtre_bdd .= "AND p.$fc = '$fv' ";
    }
    else
    {
        $filtre_bdd .= "AND cible.$fc = '$fv' ";
        $i= $i + 2;
    }
}


SQL查询:

SELECT cible.prix,a.valeur,a.idroute,cible.id,p.id,
    SUM(DISTINCT a.valeur) AS total_avances, /* the milestone sum the unique one */
    SUM(a.valeur) AS total_avance, /* the milestone sum of each row */
    SUM(cible.prix) AS total_prix /* the price sum */
FROM avances AS a,routes AS cible,projets AS p
WHERE a.idroute = cible.id AND p.id = cible.idprojet $filtre_bdd /* the conditions to join the tables and do the filter */
GROUP BY cible.id, a.idroute
HAVING total_avances <> cible.prix AND cible.prix - total_avances >- 1


现在,如何获得所有这些条件下的价格总计和里程碑总计?目标表包含项目的ID和里程碑的ID。我们必须对每一行进行里程碑总和,并将总和与项目价格进行比较。

最佳答案

尝试使用以下查询:

SELECT DISTINCT cible.prix, a.valeur, a.idroute, cible.id, p.id,
SUM(a.valeur) AS total_avances,
SUM(a.valeur) AS total_avance,
SUM(cible.prix) AS total_prix
FROM avances a INNER JOIN routes ciblE ON a.idroute = cible.id
INNER JOIN projets p ON p.id = cible.idprojet
GROUP BY cible.prix, a.valeur, a.idroute, cible.id, p.id,
HAVING total_avances <> cible.prix AND cible.prix - total_avances >- 1


希望这可以帮到你。

09-05 02:50