2014-11-14 12 views
-2

的:包括以下查詢給出結果,但我需要的是數0,如果值是空

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文件時不會發生衝突

+0

也許使用'left join'而不是'inner join'。你可以創建一個[SQLfiddle](http://sqlfiddle.com/)來演示嗎? – showdev 2014-11-14 20:21:26

+0

'有count(distinct r.restaurant_id)> 0' – 2014-11-14 20:22:25

+0

使用了左連接但是相同的結果。 – Torrezzzz 2014-11-14 20:23:29

回答

0
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; 
+0

請描述你已經改變了什麼,爲什麼。代碼轉儲比描述性答案更有幫助。 – showdev 2014-11-14 20:28:04

+0

s/inner/right在城市加入。這將爲不匹配的城市插入其他列的空值,這正是OP想要的。 – Kevin 2014-11-14 20:45:23

+0

更改我的查詢可以檢查嗎? – Torrezzzz 2014-11-15 04:50:44