mysql SUM not working with AS
i have this 3 tables my_stamp,my_invoice and stamp now i want to to get the sum of the price from stamp table. here is the sql query i have for the moment.
$sql= "SELECT s.country, s.stamp_no, s.year, s.sgno, s.condition, SUM(ssk.price) as psum ";
$sql_cnt = "SELECT s.stamp_no AS num ";
$frm = "FROM stamp AS ssk ,my_stamp AS s, my_invoice AS sk ";
$filter = "WHERE s.invoice_id=sk.invoice_id AND ssk.stamp_no=s.stamp_no AND sk.cusid='" . $usr . "'";
$sql_cnt = $sql_cnt . $frm . $filter. " Group by s.stamp_no";
the query is working fine. my main problem is psum is not getting the total price. when i loop through the result it gets the individual price.
can someone help me with this SUM in mysql? thanks.
edit::here is the final sql that i get:
SELECT s.country, s.stamp_no, s.year, s.sgno, s.condition, SUM(ssk.price) as psum FROM stamp AS ssk ,my_stamp AS s, my_invoice AS sk WHERE s.invoice_id=sk.invoice_id AND ssk.stamp_no=s.sta开发者_高级运维mp_no AND sk.cusid='10' Group by s.stamp_no ORDER BY s.country, s.sgno ASC LIMIT 0,20
Looks like you're missed GROUP BY in your query:
SELECT s.country, s.stamp_no, s.year, s.sgno, s.condition, SUM(ssk.price) as psum
GROUP BY 1, 2, 3, 4, 5
The query in $sql doesn't have a group by, so it's just going to return that rows ssk.price.
精彩评论