简体   繁体   English

获取mysql JOIN查询后的列行总和

[英]get the sum of column rows after a mysql JOIN query

$r1 = mysql_query("
SELECT *, SUM(bps.price) as sum FROM accounts 
FULL JOIN bps on bps.id = mid
");
echo "Total price: ".$row['price']."<br>" ;
    while($row = mysql_fetch_array($r1))
      {
      echo $row['name']." <b>:::</b>  ".$row['mid']." <b>:::</b>  ".$row['price'];
      echo "<br />";
      }

this is my query. 这是我的疑问。 I need to get the total(the sum) of the column : bps.price some of the rows have : 10.5 , others -5.5 etc... in other words some are negative values and others are positive.It will list all the results then at the end display the sum of column price. 我需要得到列的总和(总和):bps.price一些行有:10.5,其他-5.5等...换句话说,有些是负值,有些是正数。它会列出所有结果然后在最后显示列价的总和。

select sum(bps.price) FROM accounts a
FULL JOIN bps on bps.id = a.mid

That would work, right? 那会有效,对吗? It will sum positive and negative values, that doesn't matter. 它将总和正值和负值相加,这无关紧要。

Does this work the way you want? 这是否按您想要的方式工作? I'm not sure which columns belong to which tables, just edit the table names (a / b) as needed... 我不确定哪些列属于哪些表,只需根据需要编辑表名(a / b)...

$r1 = mysql_query("
SELECT *, sum(a.price) as sum FROM accounts as a 
FULL JOIN bps as b on a.mid = b.id
");
echo "Total price: ".$row['sum']."<br>" ;
    while($row = mysql_fetch_array($r1))
      {
      echo $row['a.name']." <b>:::</b>  ".$row['a.mid']." <b>:::</b>  ".$row['a.price'];
      echo "<br />";
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM