我有三张桌子如下所述
Main_id | Main _name
--------+-----------
1 | A
2 | B
Sub_id | Sub_name | Main_id (foreign key from main table)
-------+-----------+--------
11 | AA | 1
12 | AB | 1
22 | BB | 2
Transaction_ID | Amount | Sub_id (foreign key from sub table)
----------------+---------+---------
1 | 100 | 11
1 | 200 | 11
1 | 50 | 22
我希望每个主要帐户都有交易总额,所以它看起来像 每个主要ID及其总金额
的SQL查询是什么?Main_id | Total Amount
--------+---------------
1 | 300
2 | 50
什么是SQL查询?
答案 0 :(得分:2)
select Main_id, sum(Amount) "Total Amount"
from Table1 inner join Table2 using(Main_id)
inner join Table3 using(Sub_id)
group by Main_id
order by 1;
MAIN_ID总金额
1 300 2 50