0

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

第一个表

   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 中的以下类型

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

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

4

3 回答 3

1

您可以使用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

或者您可以使用LEFT JOIN与此类似的:

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

于 2012-11-29T11:39:50.283 回答
0
SELECT DATE, SUM(Total) totalSum
FROM
(
    SELECT * FROM tableA
    UNION ALL
    SELECT * FROM TableB
    UNION ALL 
    SELECT * FROM tableC
) s
GROUP BY DATE
于 2012-11-29T11:39:45.340 回答
0
select tbl1.date,sum(total)
from tbl1,
tbl2,
tbl3
where tbl1.date = tbl2.date and
tbl2.date = tbl3.date
group by tbl1.date;
于 2012-11-29T11:40:28.973 回答