简体   繁体   English

来自3个表的条目的总和

[英]Sum from entries of 3 Tables

I have 5 mysql tables with the same layout: 我有5个具有相同布局的mysql表:

name|||||||||points
-------------------
name1|10
name2|9
name3|11

... ...

I want to get the sum from all tables from a user so the result should be like this: 我想从用户的所有表中获取总和,因此结果应如下所示:

name|||||||||points
-------------------
name1|90
name2|59
name3|61
...

But my problem is, that some tables may not contain name1 for example, so a name can also be in 5 tables or in 1. 但是我的问题是,例如某些表可能不包含name1,因此名称也可以在5个表中或在1个表中。

SELECT Q.Name, SUM(Q.POINTS_COLUMN) Total
FROM
 (
  SELECT Name , POINTS_COLUMN  FROM TABLE_ONE
  UNION ALL 
  SELECT Name , POINTS_COLUMN  FROM TABLE_TWO
  UNION ALL 
  SELECT Name , POINTS_COLUMN  FROM TABLE_THREE
  UNION ALL 
  SELECT Name , POINTS_COLUMN  FROM TABLE_FOUR
  UNION ALL 
  SELECT Name , POINTS_COLUMN  FROM TABLE_FIVE
  ) Q
GROUP BY Q.NAME 

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

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