开发者

SQL Access help in sum from 2 different tables

i have these tables

table 1

id      price
1       30
2       40
3       50


table 2

    id      price
    1       70
    2       5
    3       10

i want a query开发者_如何学编程 that would sum the the price value based on the ID like if table1.id=table2.id then sum table1.price and table2.price the end result should be something like this

table 3

    id      price
    1       100
    2       45
    3       60


You can;

SELECT 
  TABLE1.ID, 
  TABLE1.PRICE+TABLE2.PRICE
FROM TABLE1 
  INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID;

Or if there are duplicate IDs in either table;

SELECT 
  TABLE1.ID, 
  SUM(TABLE1.PRICE+TABLE2.PRICE)
FROM TABLE1 
  INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
  GROUP BY TABLE1.ID;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜