我有3个表my_stamp,my_invoice和stamp 现在我想从邮票表中得到价格的总和。 这是我目前的SQL查询。
$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";
查询工作正常。我的主要问题是psum没有得到总价。 当我遍历结果时,它会获得单独的价格。
有人可以帮助我在mysql中使用这个SUM吗? 感谢。
edit ::这是我得到的最终sql:
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.stamp_no AND sk.cusid='10' Group by s.stamp_no ORDER BY s.country, s.sgno ASC LIMIT 0,20
答案 0 :(得分:0)
$ sql中的查询没有group by,所以它只是返回那些行ssk.price。
答案 1 :(得分:0)
您的查询中似乎错过了GROUP BY:
SELECT s.country, s.stamp_no, s.year, s.sgno, s.condition, SUM(ssk.price) as psum
GROUP BY 1, 2, 3, 4, 5