如何获得UNION ALL的总和

时间:2019-06-19 17:51:16

标签: php mysql

如何在$select = $db->prepare("SELECT SUM(money) AS cash FROM table1 WHERE money > 0 GROUP BY id UNION ALL SELECT SUM(payment) AS cash FROM table2 WHERE payment > 0 GROUP BY id UNION ALL SELECT SUM(pay) AS cash FROM table3 WHERE pay > 0 GROUP BY id "); $select ->execute(); for($i=0; $rows = $select ->fetch(); $i++){ $result = $rows['sum(cash)']; 查询中获取所有行的总和。我试图求和每个UNION表的一列,以及查询执行后的结果总和。

{{1}}

1 个答案:

答案 0 :(得分:0)

可能只有mysql的解决方案如下,它返回表中所有三列之和的单个值:

webpacker.yml

为清楚起见,

  • psum:部分和
  • pt:部分和表
  • tsum:总和