MySQL GROUP BY 不会对 NULL 进行分组统计解决办法(表述可能不正确,具体看内容...) 作者: Chuwen 时间: 2020-05-23 分类: MySQL # 数据表结构 ## goods (商品信息)表结构 ``` +------------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+-------------------+----------------+ | gdID | int(11) | NO | PRI | NULL | auto_increment | | tID | int(11) | YES | MUL | NULL | | | gdCode | varchar(50) | YES | | NULL | | | gdName | varchar(100) | NO | | NULL | | | gdPrice | float | YES | | 0 | | | gdQuantity | int(11) | YES | | 0 | | | gdSaleQty | int(11) | YES | | 0 | | | gdCity | varchar(50) | YES | | 长沙 | | | gdInfo | longtext | NO | | NULL | | | gdAddTime | timestamp | YES | | CURRENT_TIMESTAMP | | | gdHot | tinyint(11) | YES | | 0 | | | gdImage | varchar(255) | YES | | NULL | | +------------+--------------+------+-----+-------------------+----------------+ ``` ## orderdetail (商品订单信息、评论)表结构 ``` +------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------+------+-----+---------+----------------+ | odID | int(11) | NO | PRI | NULL | auto_increment | | oID | int(11) | YES | | NULL | | | gdID | int(11) | YES | | NULL | | | odNum | int(11) | YES | | NULL | | | dEvalution | varchar(8000) | YES | | NULL | | | odTime | datetime | YES | | NULL | | +------------+---------------+------+-----+---------+----------------+ ``` # 需求 > 需要列出商品信息及评价数量 > > 当时想到的解题思路是,通过查询 `OrderDetail` 表,通过外连接(RIGHT JOIN) goods 表,然后进行分组查询,然后就有了以下语句 ``` SELECT Goods.gdID, Goods.gdName, Goods.gdPrice, Goods.gdSaleQty, Goods.gdImage, COUNT( OrderDetail.gdID ) AS noe FROM `OrderDetail` RIGHT JOIN `Goods` ON `Goods`.`gdID` = `OrderDetail`.`gdID` GROUP BY `OrderDetail`.`gdID` ORDER BY noe DESC; ``` 运行结果: > 发现,当商品没有评价的,不会列出来 ``` +------+-------------+---------+-----------+--------------------------------------------------------------------------------------------------------------------+-----+ | gdID | gdName | gdPrice | gdSaleQty | gdImage | noe | +------+-------------+---------+-----------+--------------------------------------------------------------------------------------------------------------------+-----+ | 10 | 华为P9_PLUS | 3980 | 7 | 【华为官方买就送Type C 转接头】Huawei/华为 P9 plus全网通手机 | 5 | | 3 | 牛肉干 | 94 | 61 | 牛肉干一般是用黄牛肉和其他调料一起腌制而成的肉干。牛肉干中的风干牛肉源于蒙古铁骑的战粮,携带方便,并且有丰富的营养 | 2 | | 5 | 运动鞋 | 400 | 200 | 运动,健康等 | 1 | | 6 | 咖啡壶 | 50 | 45 | 一种冲煮咖啡的器具。咖啡壶是欧洲最早的发明之一,约在1685年于法国问世,在路易十五时期在各地广为流传。 | 1 | | 8 | A字裙 | 128 | 200 | 2016秋季新品韩版高腰显瘦圆环拉链a字半身裙双口袋包臀短裙子女 | 1 | | 1 | 迷彩帽 | 63 | 29 | 透气夏天棒球帽男女鸭舌帽网帽迷彩帽子太阳帽防晒韩版休闲遮阳帽 | 0 | +------+-------------+---------+-----------+--------------------------------------------------------------------------------------------------------------------+-----+ ``` # 解决 再网上找到了一个类似的问题:https://blog.csdn.net/u011277123/article/details/79883855 这篇文章参考的是:https://stackoverflow.com/questions/4588935/group-by-do-not-group-null 他是这么写的 > 增加一个 UUID(),把 b.name 的 NULL 的值都转化为具有专一性的 uuid,这样每个 b.name 都会不同, GROUP 就不会对他们进行分组 我也没怎么理解他说的,但是按照他的方法做,可以实现我想要的结果 SQL 语句: ``` SELECT Goods.gdID, Goods.gdName, Goods.gdPrice, Goods.gdSaleQty, Goods.gdImage, COUNT( OrderDetail.gdID ) AS noe FROM `OrderDetail` RIGHT JOIN `Goods` ON `Goods`.`gdID` = `OrderDetail`.`gdID` GROUP BY IFNULL( `OrderDetail`.`gdID`, UUID()); ORDER BY noe DESC; ``` 运行结果: > 评价数为 0 的结果也输出了,这正是我想要的结果 ``` +------+-------------+---------+-----------+--------------------------------------------------------------------------------------------------------------------+-----+ | gdID | gdName | gdPrice | gdSaleQty | gdImage | noe | +------+-------------+---------+-----------+--------------------------------------------------------------------------------------------------------------------+-----+ | 10 | 华为P9_PLUS | 3980 | 7 | 【华为官方买就送Type C 转接头】Huawei/华为 P9 plus全网通手机 | 5 | | 3 | 牛肉干 | 94 | 61 | 牛肉干一般是用黄牛肉和其他调料一起腌制而成的肉干。牛肉干中的风干牛肉源于蒙古铁骑的战粮,携带方便,并且有丰富的营养 | 2 | | 5 | 运动鞋 | 400 | 200 | 运动,健康等 | 1 | | 6 | 咖啡壶 | 50 | 45 | 一种冲煮咖啡的器具。咖啡壶是欧洲最早的发明之一,约在1685年于法国问世,在路易十五时期在各地广为流传。 | 1 | | 8 | A字裙 | 128 | 200 | 2016秋季新品韩版高腰显瘦圆环拉链a字半身裙双口袋包臀短裙子女 | 1 | | 1 | 迷彩帽 | 63 | 29 | 透气夏天棒球帽男女鸭舌帽网帽迷彩帽子太阳帽防晒韩版休闲遮阳帽 | 0 | +------+-------------+---------+-----------+--------------------------------------------------------------------------------------------------------------------+-----+ ``` 标签: MySQL