我想将左联接转换为view table
。
Following is my SQL
SELECT d.account_no, COALESCE(d.deposits, 0) - COALESCE(w.withdrawals, 0)
AS balance
FROM (SELECT account_no, SUM(deposit_amount) AS deposits
FROM deposits
GROUP BY account_no) d
LEFT JOIN (SELECT account_no, SUM(withdraw_amount) AS withdrawals
FROM withdraws
GROUP BY account_no) w ON w.account_no = d.account_no
我想创建view
表。
我该怎么做?有人可以帮忙吗?
答案 0 :(得分:0)
创建视图(一次)并在左联接中使用
CREATE VIEW my_view AS
SELECT account_no, SUM(withdraw_amount) AS withdrawals
FROM withdraws
GROUP BY account_no
;
SELECT d.account_no, COALESCE(d.deposits, 0) - COALESCE(w.withdrawals, 0)
AS balance
FROM (SELECT account_no, SUM(deposit_amount) AS deposits
FROM deposits
GROUP BY account_no) d
LEFT JOIN my_view w ON w.account_no = d.account_no