INNER JOIN:添加具有相同ID的金额

时间:2014-07-03 13:25:02

标签: mysql sql join inner-join

如果客户ID(KUNDENID)相同,我会尝试从表中累加金额。

这是我的两张桌子:

+----------+------------+--------+
| KUNDENID | datum      | Betrag |
+----------+------------+--------+
|        2 | 2013-06-05 |  120.5 |
|        1 | 2013-02-05 |  20.95 |
|        3 | 2013-02-05 |  250.3 |
|        3 | 2013-05-13 |    100 |
|        4 | 2013-01-01 |   1500 |
+----------+------------+--------+

+----------+---------+-----------+------------------------------+--------------+-------------+
| KUNDENID | vorname | nachname  | email                        | geburtsdatum | telefon     |
+----------+---------+-----------+------------------------------+--------------+-------------+
|        1 | Homer   | Simpson   | chunkylover53@aol.com        | 1956-05-02   | 555-364     |
|        2 | Doug    | Heffernan | doug.heffernan@hotmail.com   | 1965-02-09   | 555-3684    |
|        3 | Dexter  | Morgan    | dexter.morgan@gmail.com      | 1971-02-01   | 555-3684    |
|        4 | Sheldon | Cooper    | sheldoncooper@gmail.com      | 1981-04-22   | 555-2274648 |
|        5 | Maurice | Moss      | moss@Reynholm-Industries.com | 1972-03-01   | 555-6677    |
+----------+---------+-----------+------------------------------+--------------+-------------+

我试过这个INNER JOIN:

SELECT k.KUNDENID, vorname, nachname, SUM(Betrag) FROM kunde k INNER JOIN verkauf v ON k.KUNDENID = v.KUNDENID GROUP BY k.KUNDENID;

输出:

+----------+---------+-----------+------------------+
| KUNDENID | vorname | nachname  | SUM(Betrag)      |
+----------+---------+-----------+------------------+
|        1 | Homer   | Simpson   | 20.9500007629395 |
|        2 | Doug    | Heffernan |            120.5 |
|        3 | Dexter  | Morgan    | 350.300003051758 |
|        4 | Sheldon | Cooper    |             1500 |
+----------+---------+-----------+------------------+

所以我知道JOIN是正确的,但是如何向同一个JOIN中没有购买任何东西的顾客展示呢?

我想要的输出:

+----------+---------+-----------+------------------+
| KUNDENID | vorname | nachname  | SUM(Betrag)      |
+----------+---------+-----------+------------------+
|        1 | Homer   | Simpson   | 20.9500007629395 |
|        2 | Doug    | Heffernan |            120.5 |
|        3 | Dexter  | Morgan    | 350.300003051758 |
|        4 | Sheldon | Cooper    |             1500 |
|        5 | Maurice | Moss      |                0 |
+----------+---------+-----------+------------------+

感谢您的帮助!

4 个答案:

答案 0 :(得分:4)

使用LEFT JOIN代替INNER JOIN

SELECT k.KUNDENID, vorname, nachname, IFNULL(SUM(Betrag), 0) AS gesamt
    FROM kunde k
    LEFT JOIN verkauf v ON k.KUNDENID = v.KUNDENID
    GROUP BY k.KUNDENID;

这是Wikipedia的基本解释。他们也展示了一个例子。

  

表A和表的左外连接(或简称左连接)的结果   B总是包含“左”表(A)的所有记录,即使是   join-condition在“右”表中找不到任何匹配的记录   (B)。这意味着如果ON子句匹配B中的0(零)记录   (对于A中的给定记录),连接仍将返回一行   结果(对于那条记录) - 但是在B的每一列中都是NULL。左边   outer join返回内部联接中的所有值以及所有值   在左表中与右表不匹配。

答案 1 :(得分:2)

是的,您可以使用Left outer join代替Inner Join Like:

SELECT k.KUNDENID, vorname, nachname, SUM(Betrag) AS Betrag  
FROM kunde k LEFT OUTER JOIN verkauf v 
       ON k.KUNDENID = v.KUNDENID GROUP BY k.KUNDENID;

答案 2 :(得分:1)

内部连接:查询将table1的每一行与table2的每一行进行比较,以查找满足连接谓词的所有行对。

左外连接会保留“左”表的所有行,无论“右”表中是否有匹配的行。

试试这个:

SELECT k.KUNDENID, vorname, nachname, SUM(Betrag) as Betrag
    FROM kunde k LEFT OUTER JOIN verkauf v 
     ON k.KUNDENID = v.KUNDENID 
     GROUP BY k.KUNDENID;

答案 3 :(得分:1)

我不知道kunde tableverkauf table在哪里,但是如果查询错误或错误,则jsut切换表名称

SELECT a.KUNDENID,IFNULL(SUM(b.betrag),0) AS sum , a.nachname FROM kunde a
LEFT JOIN verkauf b ON b.KUNDENID = a.KUNDENID
GROUP BY b.KUNDENID
ORDER BY a.KUNDENID