2015-02-03 8 views
1

나는 그 테이블이 있습니다SUM MYSQL

alltransations

enter image description here

표를 alladdon_list

enter image description here

enter image description here

하지만 내 query과 :

<table border=1 cellpadding=5 > 
    <tr> 
     <th>Names</th> 
     <th>Amount</th> 
    </tr> 

<?php 

include("conn_archive.php"); 

$cfaddontotal = 0; 

$getall = mysql_query("SELECT *, sum(quantity) as quantity,price 
         FROM alltransactions at, alladdon_list aal 
         WHERE at.transac_id=aal.aatransac_id 
         GROUP BY guest_id 
         ", $db2); 
while($rows = mysql_fetch_assoc($getall)){ 
     // GUEST INFO 
     $guest_fname = $rows['guest_fname']; 
     $guest_lname = $rows['guest_lname']; 

     // ADDON INFO 
     $addondesc = $rows['description']; 
     $addondate = $rows['datef']; 
     $addonqty = $rows['quantity']; 
     $addonprice = $rows['price']; 
     $addontcost = $addonprice * $addonqty; // cost of specific product 
     $cfaddontotal += $addonprice * $addonqty; // total cost of all products 

     echo "<tr>"; 
     echo "<td>$guest_fname $guest_lname</td>"; 
     echo "<td>&#8369; ".number_format($addontcost)."</td>"; 
     echo "</tr>"; 
} 

?> 
<tr> 
    <th colspan=2 >Total Addon Cost: &#8369; <?php echo number_format($cfaddontotal); ?></th> 
</tr> 
</table> 

내가 가진이 난 그냥 내가이 같은 결과를 얻을 수있는 특정 transac_idguest_id에 의해 group을 간단 sum까지의 모든 애드온 원하는 (틀 렸습니다) :

enter image description here

query을 사용하여 정확한 계산을 수행 할 수 없습니다.

답변

2

코드가 혼동합니다. 귀하는 모든 상품의 가격을 합산하여 하나의 "무작위"가격을 곱합니다 (group by을 선택하면 집계 기능이없는 필드가 포함되어있는 것이 좋을 것입니다 : MySQL Handling of GROUP BY).

그것은 뭔가

SELECT *, sum(quantity*price) as total 
        FROM alltransactions at, alladdon_list aal 
        WHERE at.transac_id=aal.aatransac_id 
        GROUP BY guest_id 
+0

오에 쿼리를 변경하는 것이 좋습니다. 그만큼 단순한가? 그걸 알아낼 수 없어요. 감사. 그것은 작동합니다 :) –