Mysql计数,返回所有字段,如果没有计数则返回0

时间:2012-11-07 14:54:12

标签: mysql count null return zero

我一直在寻找有很多人遇到同样问题的人,但解决方案似乎有了更大的不同?!

我有3张桌子:

+-------------------------------------------+
| ITEM: itemid(pk), status(fk), owner(fk)   |
|                                           |
| STATUS: statusid(pk), statusname          |
|                                           |
| OWNER: ownerid(pk), ownername             |
+-------------------------------------------+

我有2个状态: 1:可用 2:已损坏

在这种情况下,只有一个拥有一个破损项目的所有者。 我正在使用此代码。首先它是不同的,但似乎LEFT JOIN和GROUP / ORDER BY应该做的伎俩。不幸的是我。

SELECT ownername, SUM(price) AS  'totalbrokenpriceeach', COUNT( itemid ) AS  'totalbrokenitemseach'
                                FROM item
                                LEFT JOIN owner ON item.owner = owner.ownerid
                                LEFT JOIN status on item.status = status.statusid
                                WHERE statusid='2'
                                GROUP BY ownerid

返回:

ownername   totalbrokenpriceeach    totalbrokenitemseach
Owner #1            60                          1
Owner #5            180                         4

我想回来:

ownername   totalbrokenpriceeach    totalbrokenitemseach
Owner #1            60                          1
Owner #2            0                           0
Owner #3            0                           0
Owner #4            0                           0
Owner #5            180                         4

怎么办?任何解决方案?



编辑:

         OWNER                          
+-----------------------+
| ownerid ownername     |
|     1:    Henk        |
|     2:    Jan         |
|     3:    Piet        |
|     4:    Klaas       |
+-----------------------+

         STATUS
+-----------------------+
| statusid statusbeschr |
|     1:    Available   |
|     2:    Broken      |
+-----------------------+

         ITEM
+--------------------------------------+
| itemid    price     owner     status |
|     1:    90          1          1   |
|     2:    40          2          1   |
|     3:    20          2          1   |
|     4:    120         3          2   |
+--------------------------------------+

I need returned:

+-------------------------------------------------------+
| ownername    SumOfBrokenItems   AmountOfBrokenItems   |
|     Henk:       0                       0             |
|     Jan:        0                       0             |
|     Piet:       120                     1             |
|     Klaas:      0                       0             |
+-------------------------------------------------------+

1 个答案:

答案 0 :(得分:3)

<击>

<击>
SELECT  a.ownername, 
        SUM(price) AS  totalbrokenpriceeach, 
        COUNT(b.itemid) AS  totalbrokenitemseach
FROM    owner a
        LEFT JOIN   item b
            ON a.ownerID = b.owner
        LEFT JOIN   status c
            ON b.status = c.statusID AND 
               c.StatusID = 2
GROUP BY    ownername

<击>

更新1

SELECT  a.ownername, 
        SUM(CASE WHEN c.statusbeschr = 'Broken' THEN b.price ELSE 0 END) AS  totalbrokenpriceeach, 
        SUM(CASE WHEN c.statusbeschr = 'Broken' THEN 1 ELSE 0 END) AS  totalbrokenitemseach
FROM    owner a
        LEFT JOIN   item b
            ON a.ownerID = b.owner
        LEFT JOIN   status c
            ON b.status = c.statusID        
GROUP BY a.ownerid
ORDER BY a.ownerid