根据SQL中另一个表的组合求和

时间:2019-03-07 11:13:03

标签: sql

我有两个表:

表1销售商品

+-------+-------+
| Items | Sales |
+-------+-------+
| A     | 34    |
+-------+-------+
| B     | 22    |
+-------+-------+
| C     | 10    |
+-------+-------+

表2

组合

+-------+-------+
| item1 | item2 |
+-------+-------+
| A     | B     |
+-------+-------+
| C     | B     |
+-------+-------+
| C     | A     |
+-------+-------+

我想要表1中项1和项2的组合值之和 例如A + B

2 个答案:

答案 0 :(得分:2)

请尝试这个。

SELECT(A.Sales + B.Sales) AS Total, * FROM Combination
INNER JOIN ITEM A ON A.Items = C.Item1
INNER JOIN ITEM  B ON  B.Items = C.Item2

答案 1 :(得分:1)

将组合表与项目表结合两次

select b.sales+c.sales as totalval from combination a
inner join item b on a.item1=b.Items
inner join item c on a.item2=c.Items