sum from both mysql table columns

后端 未结 1 1752
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 13:05

I have two mysql tables:

table: loan
--------------------------------------------------------
 id | advance_id | loan_amount | deposit_amount | date
------------         


        
1条回答
  •  借酒劲吻你
    2021-01-25 13:12

    you can rewrite your query as

    SELECT 
      a.advance_id,
      SUM(a.advance_amount) AS adv_amount,
      a.purpose,
      a.date,
      l.loan_amount 
    FROM
      advance AS a 
      INNER JOIN 
      (
        SELECT advance_id,SUM(deposit_amount) AS loan_amount
        FROM loan
        GROUP BY advance_id
        HAVING SUM(loan_amount) - SUM(deposit_amount) > 0 
      )  AS l USING(advance_id)
    GROUP BY a.advance_id 
    

    DEMO

    0 讨论(0)
提交回复
热议问题