Sum of All Related Rows with Matching ID MySQL

后端 未结 2 1191
忘掉有多难
忘掉有多难 2021-01-21 13:04

I have the following table schema:

    tbl_portfolio
    ----------
    id (auto number)
    name

-

    tbl_registration
    --         


        
2条回答
  •  野的像风
    2021-01-21 13:41

    Have you tried using SUM()?

    SELECT port.*, SUM(trans.shares * trans.price) AS transaction_totals
      FROM tbl_portfolio port
      INNER JOIN tbl_registration reg ON reg.portfolio_id = port.id
      LEFT JOIN tbl_fund fund on fund.registration_id = reg.id
      LEFT JOIN tbl_transaction trans ON trans.fund_id = fund.id
      GROUP BY port.id
    

提交回复
热议问题