Table Products :  TID , cost
Table Parts    :  PID , TID , quantity已知 PID , 从 表 Parts 中获取 list of TID ,求和 costSum = Sum(cost * quantity)谢谢了

解决方案 »

  1.   

    SELECT pid,sum(products.cost*parts.quantity) from products,parts where products.tid=parts.tid
      

  2.   

    select parts.pid,sum((isNull(products.cost),0))*parts.quantity) from parts left join products on parts.tid=products.tid group by  parts.pid
      

  3.   

    理论上select sum(products.cost * parts.quantity) from products where TID = { select TID from parts where PID = 'somepid' }  有许多 TID
      

  4.   

    /* mysql里边应该使用IFNULL */select parts.pid,
           sum(IFNull(products.cost,0)*parts.quantity) 
    from parts left join products 
      on parts.tid=products.tid 
    group by parts.pid
    having parts.pid='somepid'
      

  5.   

    select parts.pid, sum(IFNull(products.cost,0)*parts.quantity) from parts left join products on parts.tid=products.tid group by parts.pid having parts.pid='somepid'