donglusou3335 2014-06-15 10:26
浏览 175
已采纳

在MYSQL中,我希望获得具有多个条件的多个SUM来完成总计并获得价格里程碑

Here is my SQL code. I have selected the price of each row and the milestone of each row and the id to join the tables, but I have only the result of one row.

PHP code to do multiple filters:

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

SQL query:

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 

Now how can I get the totals of the prices and the total of the milestones with all those conditions? The targeted table contains the id of the project and the id of the milestones. We have to do the sum of the milestones for each row and sum of the sums and compare it to the project price.

  • 写回答

2条回答 默认 最新

  • dpnru86024 2014-06-17 12:45
    关注

    i found the solution thank you

    SELECT SUM(alucard.uu) as total_prix,SUM(alucard.vv) as total_avances FROM(
                                    SELECT ultimate.id,SUM(ultimate.prix) as uu,SUM(ultimate.valeur) as vv FROM(
                                        SELECT e.id,e.prix,e.idprojet,e.valeur FROM(
                                            SELECT cible.id,cible.prix,cible.idprojet,a.valeur=4 AS valeur FROM avances AS a
                                                INNER JOIN routes cible ON a.idroute=cible.id 
                                                INNER JOIN projets p ON p.id=cible.idprojet $filtre_bdd 
                                                 $date_bdd
                                                GROUP BY cible.id) AS e
                                        UNION 
    
                                        SELECT cible.id,cible.prix=4,cible.idprojet,SUM(a.valeur) FROM avances as a
                                            INNER JOIN routes cible ON a.idroute=cible.id 
                                            INNER JOIN projets p ON p.id=cible.idprojet $filtre_bdd 
                                            $date_bdd
                                            GROUP BY cible.id) AS ultimate
                                        GROUP BY ultimate.id
                                        HAVING SUM(ultimate.prix)>SUM(ultimate.valeur) ) AS alucard
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度