在 PHP 中计算所有表格行字段的总和 [英] Make sum of all table row field in PHP

查看:66
本文介绍了在 PHP 中计算所有表格行字段的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 4 个表,其中所有表都有 3 行 [记录] 和 3 列 [字段] 字段,如日期名称总计.此表如下:

I have 4 tables, in this all tables have 3 rows[record] and 3 column[field] field like Date Name Total. This table like below:

第一张桌子

   Date                  Name                      Total
 2012-11-07             Manoj                       10
 2012-11-08             Ganesh                       4
 2012-11-09             Sri                         30

第二表

  Date                   Name                      Total
2012-11-07               Raju                         5
2012-11-08               ggg                          3
2012-11-09               Shshi                       30

第三张桌子

 Date                   Name                      Total
2012-11-07              Ram                         2
2012-11-08               gm                         5
2012-11-09             Shsse                       30

我需要像 PHP 中的以下类型输出

I need Output Like following type in PHP

Date          Total
2012-11-07      17
2012-11-08      12
2012-11-09      90

它应该在 PHP 中显示日期之间的所有总数

It should display all total in between date in PHP

推荐答案

您可以使用 UNION ALL:

select date, sum(total) AllTotal
from
(
  select date, total
  from table1
  union all
  select date, total
  from table2
  union all
  select date, total
  from table3
) src
group by date

参见SQL Fiddle with Demo

或者你可以使用类似这样的LEFT JOIN:

Or you might be able to use a LEFT JOIN similar to this:

select t1.date, sum(t1.total + t2.total + t3.total) AllTotal
from table1 t1
left join table2 t2
  on t1.date = t2.date
left join table3 t3
  on t1.date = t3.date
group by t1.date

参见SQL Fiddle with Demo

这篇关于在 PHP 中计算所有表格行字段的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆