Sum total of table with two related tables

前端 未结 2 685
情深已故
情深已故 2021-01-26 09:49

I\'m here with this (I\'m sure it is) simple question I can\'t figure out how to solve.

I have this schema:

<script

2条回答
  •  暖寄归人
    2021-01-26 10:02

    SELECT name, IFNULL(f.total, 0) AS total_fruit, IFNULL(c.total, 0) AS total_cookie
    FROM person AS p
    LEFT JOIN (SELECT person_idperson, SUM(cost) AS total
               FROM fruit
               GROUP BY person_idperson) AS f
    ON p.idperson = f.person_idperson
    LEFT JOIN (SELECT person_idperson, SUM(cost) AS total
               FROM cookie
               GROUP BY person_idperson) AS c
    ON p.idperson = c.person_idperson
    

提交回复
热议问题