MySQL SELECT全部与LEFT JOIN和WHERE组合在一起

时间:2016-10-14 17:45:20

标签: mysql left-join where-clause

当前结果

SELECT CT.cat2 as Cat, SUM(MT.`revenue`) as Revenue
                FROM sales MT
                    LEFT JOIN `2016productcodes` CT 
                        ON MT.productsku = CT.productcode 
                WHERE MT.`date` BETWEEN '2016-10-01' AND '2016-10-13' AND revenue > 0
                GROUP BY CT.cat2

Cat        | Revenue   |
---------------------------
cat1       | 250.00    |
cat3       | 100.00    |
cat5       | 32.00     | 

我想显示所有分组类别(当没有收入时显示NULL,BLANK或“0.00”无关紧要。)

期望的结果

Cat        | Revenue   |
---------------------------
cat1       | 250.00    |
cat2       |           |
cat3       | 100.00    | 
cat4       |           |
cat5       | 32.00     | 

我已尝试过此查询,它确实显示了所有类别,但它将每个类别中的所有收入分组(收入始终相同)。

SELECT cat2 as Cat, (SELECT
        SUM(MT.`revenue`)
                FROM 2016productcodes CT 
                    LEFT JOIN `sales` MT
                        ON MT.productsku = CT.productcode

                WHERE MT.`date` BETWEEN '2016-10-01' AND '2016-10-13'

           ) Revenue
FROM 2016productcodes
GROUP BY cat2

0 个答案:

没有答案