Sum total of table with two related tables

前端 未结 2 681
情深已故
情深已故 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
    
    0 讨论(0)
  • 2021-01-26 10:03
    SELECT p.name AS PERSON_NAME, 
        IFNULL(SUM(f.cost),0) AS TOTAL_FRUIT, 
        IFNULL(SUM(c.cost),0) AS TOTAL_COOKIE
    FROM person AS p
    LEFT JOIN fruit as f
        ON p.idperson = f.person_idperson
    LEFT JOIN cookie as c
        ON p.idperson = c.person_idperson
    GROUP BY p.idperson
    
    0 讨论(0)
提交回复
热议问题