2016-06-11 7 views
1

マイクエリIFNULL()は、MySQLを使用して、列の値が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(列、見つからない) ' – 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

作業中です。どうもありがとう。 – Gilzy

+0

あなたはようこそ@Gilzy –

1

が見つからなかったすべてのために0を取得するにはleft join使用

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 
関連する問題