Mysql 如果值为空,则包括计数0

Mysql 如果值为空,则包括计数0,mysql,Mysql,下面的查询给出了结果,但我需要的是: city_name | total | +-----------+-------+ | Bangalore | 13 | | Mumbai | 21 | | Coimbatore| 0 | | Madurai | 0 | | salem | 0 | | Chennai | 4 | | Pune | 30 | | Ghaziabad | 1 | | Gurgaon |

下面的查询给出了结果,但我需要的是:

city_name | total |
+-----------+-------+
| Bangalore |    13 |
| Mumbai    |    21 |
| Coimbatore|     0 |
| Madurai   |     0 |
| salem     |     0 |
| Chennai   |     4 |
| Pune      |    30 |
| Ghaziabad |     1 |
| Gurgaon   |     2 |
但查询结果如下:

+-----------+-------+
| city_name | total |
+-----------+-------+ 
| Bangalore |    13 |
| Mumbai    |    21 |
| Chennai   |     4 |
| Pune      |    30 |
| Ghaziabad |     1 |
| Gurgaon   |     2 |
+-----------+-------+
查询:

select c.city_name,count(distinct r.restaurant_id) as total from restaurants r
                                    left join je_restaurant_status jrs on r.restaurant_id = jrs.restaurant_id
                                    left join locations l on l.location_id = r.location_id
                                    left outer join cities c on c.city_id = l.city_id                                                                                              where jrs.update_date < '2014-10-01 00:00:00'                        
                                     and jrs.registered=1 and jrs.operations_closed = 0 and jrs.temporary_disabled=1                                                               group by c.city_id

我可以使用哪个函数包含mysql中的空值。我希望此查询与我的其他查询结果匹配,以便在CSV文件中转储时不会发生冲突

可能使用左连接而不是内部连接。您是否可以创建一个用于演示的?使用countdistinct r.restaurant\u id>0进行左联接,但结果相同。使用countdistinct r.restaurant\u id>0仍然会给出相同的结果。您是否可以导出至少您的架构,以便我们可以复制?A会有帮助的。请描述一下你改变了什么以及为什么。代码转储不如描述性答案有用。城市连接中的s/inner/right。这将在其他不匹配的城市列中插入空值,这正是OP想要的。更改了我的查询,你能检查一下吗?@Torrezzzz:不,这不起作用。如果要使用左联接,城市表必须位于联接的左侧。如果要使用右联接,城市表必须位于右侧。在本例中,我使用了右连接来避免对表进行重新排序。
select c.city_name,count(distinct r.restaurant_id) as total from restaurants r                                   
 inner join status jrs on r.restaurant_id = jrs.restaurant_id                                        
 inner join locations l on l.location_id = r.location_id                                          
 right join cities c on c.city_id = l.city_id                                                                             
 where jrs.update_date < '2014-10-01 00:00:00'and  
 jrs.registered=1 and jrs.operations_closed = 0                                          
 group by c.city_id;