简体   繁体   English

将具有行和列的两个表连接起来并求和

[英]Joining two table with rows and column and sum them

I have a table that looks like 我有一张桌子,看起来像

ColA  ColB  ColC ColD  ColE  ColF
 A    B      C    D     E      F

I want to join each rows of this table to another table 我想将此表的每一行连接到另一个表

Column1  values
 A          1 
 A          2
 B          1 
 B          2

The resulting output will be sum of the column values from table 2. For example, First row of table 1 has A,B that are present in table 2 and their combined sum is 6. 结果输出将是表2中列值的总和。例如,表1的第一行具有表2中存在的A,B,它们的总和为6。

ColA  ColB  ColC ColD  ColE  ColF    sum
 A    B      C    D     E      F      6

Any ideas how to do that in MYSQL? 任何想法如何在MYSQL中做到这一点?

You can do this with a subquery: 您可以使用子查询来执行此操作:

select t.*,
       (select sum(t2.value)
        from t2
        where t2.column1 in (t.cola, t.colb, t.colc, t.cold, t.cole, t.colf)
       ) as total
from t;

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

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