MySQL-按空值分组

MySQL-按空值分组,mysql,sql,Mysql,Sql,我有以下SQL查询: SELECT date_format(sfo.shipping_arrival_date,"%m.%Y") AS Month, date_format(sfo.shipping_arrival_date,"%v") AS Week, date_format(sfo.shipping_arrival_date,"%d.%m.%Y") AS Shipping Day, CASE WHEN sfo.coupon_co

我有以下SQL查询:

SELECT date_format(sfo.shipping_arrival_date,"%m.%Y") AS Month,
       date_format(sfo.shipping_arrival_date,"%v") AS Week,
       date_format(sfo.shipping_arrival_date,"%d.%m.%Y") AS Shipping Day,
       CASE
           WHEN sfo.coupon_code IS NOT NULL THEN sfo.coupon_code
           ELSE 'no Code'
       END as Coupon,
       sfo.coupon_rule_name,
       sfo.grand_total AS Endsumme,
       sfo.base_subtotal + sfo.shipping_incl_tax AS Sum_Incl_Shipping,
       sfo.discount_amount,
       count(sfo.entity_id) AS uses
FROM sales_flat_order AS sfo
WHERE sfo.status <> 'canceled'
GROUP BY Coupon
ORDER BY sfo.shipping_arrival_date DESC
选择日期格式(sfo.装运/到达日期,“%m.%Y”)作为月份,
日期格式(sfo.装运日期、到达日期、%v)为周,
日期格式(sfo.装运/到达日期,“%d.%m.%Y”)作为装运日期,
案例
当sfo.优惠券代码不为空时,则为sfo.优惠券代码
否则“无代码”
以优惠券结束,
sfo.优惠券规则名称,
sfo.grand_总计作为最终汇总,
sfo.base_小计+sfo.SHIPING_(含税)作为总额(含运费),
sfo.贴现金额,
使用时的计数(sfo.实体id)
来自作为sfo的销售订单
其中sfo.status“已取消”
按优惠券分组
按sfo订购。发货\到货\日期说明
在sfo.U代码中,存在值“NULL”,但查询不会在结果中将其列为“无代码”。我想要一个分组优惠券代码和相关值的列表

有人知道为什么我在结果中找不到“无代码”值吗


谢谢。

我的直觉是,对于带有
sfo.status=NULL
的记录,您只有
NULL
优惠券(优惠券)代码
值,这些值从结果集中被忽略

尝试以下查询:

SELECT date_format(sfo.shipping_arrival_date,"%m.%Y") AS Month,
       date_format(sfo.shipping_arrival_date,"%v") AS Week,
       date_format(sfo.shipping_arrival_date,"%d.%m.%Y") AS Shipping Day,
       CASE
           WHEN sfo.coupon_code IS NOT NULL THEN sfo.coupon_code
           ELSE 'no Code'
       END as Coupon,
       sfo.coupon_rule_name,
       sfo.grand_total AS Endsumme,
       sfo.base_subtotal + sfo.shipping_incl_tax AS Sum_Incl_Shipping,
       sfo.discount_amount,
       count(sfo.entity_id) AS uses
FROM sales_flat_order AS sfo
WHERE sfo.status <> 'canceled' OR sfo.status IS NULL
GROUP BY Coupon
ORDER BY sfo.shipping_arrival_date DESC
选择日期格式(sfo.装运/到达日期,“%m.%Y”)作为月份,
日期格式(sfo.装运日期、到达日期、%v)为周,
日期格式(sfo.装运/到达日期,“%d.%m.%Y”)作为装运日期,
案例
当sfo.优惠券代码不为空时,则为sfo.优惠券代码
否则“无代码”
以优惠券结束,
sfo.优惠券规则名称,
sfo.grand_总计作为最终汇总,
sfo.base_小计+sfo.SHIPING_(含税)作为总额(含运费),
sfo.贴现金额,
使用时的计数(sfo.实体id)
来自作为sfo的销售订单
其中sfo.status“已取消”或sfo.status为空
按优惠券分组
按sfo订购。发货\到货\日期说明

我的直觉是,对于带有
sfo.status=NULL
的记录,您只有
NULL
优惠券(优惠券)代码
值,这些值从结果集中被忽略

尝试以下查询:

SELECT date_format(sfo.shipping_arrival_date,"%m.%Y") AS Month,
       date_format(sfo.shipping_arrival_date,"%v") AS Week,
       date_format(sfo.shipping_arrival_date,"%d.%m.%Y") AS Shipping Day,
       CASE
           WHEN sfo.coupon_code IS NOT NULL THEN sfo.coupon_code
           ELSE 'no Code'
       END as Coupon,
       sfo.coupon_rule_name,
       sfo.grand_total AS Endsumme,
       sfo.base_subtotal + sfo.shipping_incl_tax AS Sum_Incl_Shipping,
       sfo.discount_amount,
       count(sfo.entity_id) AS uses
FROM sales_flat_order AS sfo
WHERE sfo.status <> 'canceled' OR sfo.status IS NULL
GROUP BY Coupon
ORDER BY sfo.shipping_arrival_date DESC
选择日期格式(sfo.装运/到达日期,“%m.%Y”)作为月份,
日期格式(sfo.装运日期、到达日期、%v)为周,
日期格式(sfo.装运/到达日期,“%d.%m.%Y”)作为装运日期,
案例
当sfo.优惠券代码不为空时,则为sfo.优惠券代码
否则“无代码”
以优惠券结束,
sfo.优惠券规则名称,
sfo.grand_总计作为最终汇总,
sfo.base_小计+sfo.SHIPING_(含税)作为总额(含运费),
sfo.贴现金额,
使用时的计数(sfo.实体id)
来自作为sfo的销售订单
其中sfo.status“已取消”或sfo.status为空
按优惠券分组
按sfo订购。发货\到货\日期说明

我会使用COALESCE而不是CASE

...
COALESCE(sfo.coupon_code,'no Code') AS Coupon
...
GROUP BY Coupon

。。。因为COALESCE表达式是CASE表达式的一种语法快捷方式,因此更加清晰和简洁

我会使用COALESCE而不是CASE

...
COALESCE(sfo.coupon_code,'no Code') AS Coupon
...
GROUP BY Coupon

。。。因为合并表达式是大小写表达式的一种语法捷径,因此更加清晰和简洁

结果是什么?优惠券中是否返回相同的空值?如果省略GROUP BY子句,优惠券代码是否有“无代码”的结果?此结果集中可能没有空值。我在结果集中没有“无代码”。当我删除GROUPBY子句时,我看到一行有“no Code”,但其他每一列都是null。结果如何?优惠券中是否返回相同的空值?如果省略GROUP BY子句,优惠券代码是否有“无代码”的结果?此结果集中可能没有空值。我在结果集中没有“无代码”。当我删除GROUPBY子句时,我看到一行带有“no Code”,但其他每一列都是null。