2016-06-11 28 views
1

我查詢可以使用MySQL,IFNULL()返回列值爲int的字符串嗎?

select cname, count(screening_occapancy.idclient) as 'Count' 
from client, screening_occapancy 
where client.client_no = screening_occapancy.idclient 
group by cname 

返回如下:

Name  Count 
Name1  2 
Name2  3 
Name3  6 

等,現在我想在「計數」被「找不到」如果值爲null或0的值,是可能?我需要在我的結果類似的東西:

Name  Count 
    Name1   2 
    Name2   3 
    Name3 "Not found" 
+0

標題問題的答案是肯定的:'ifnull(column,「Not Found」)' – Barmar

回答

1
Select cname , 
case when Count is null or count =0 then 'Not found' 
else Count end as count 
from 
    (select cname, 
count(screening_occapancy.idclient) as 'Count' 
    from client left join screening_occapancy 
    on 
    client.client_no = screening_occapancy.idclient group by cname) t 

編寫一個包裝查詢查詢上述檢查數列

+0

Working。非常感謝。 – Gilzy

+0

不客氣@Gilzy –

1

使用left join獲得0所有未找到匹配

select c.cname, 
     count(so.idclient) as 'Count' 
from client c 
left join screening_occapancy so on c.client_no = so.idclient 
group by c.cname 

而且BTW不使用傳統的隱含了連接語法。使用顯式連接。

0
select cname, IF(count(screening_occapancy.idclient)!=0,count(screening_occapancy.idclient),'NOT FOUND') as 'Count' 
from client, screening_occapancy 
where client.client_no = screening_occapancy.idclient 
group by cname 

,或者如果計數返回null?

select cname, IFNULL(count(screening_occapancy.idclient),'NOT FOUND') as 'Count' 
from client, screening_occapancy 
where client.client_no = screening_occapancy.idclient 
group by cname