1

My query

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

returns the following:

Name        Count
Name1        2
Name2        3
Name3        6

etc, now I want it the value in 'Count' to be "not found" if the value is null or 0, is that possible? I need something like that in my results:

    Name        Count
    Name1         2
    Name2         3
    Name3    "Not found"
4

3 回答 3

1

Use a left join to get 0 for all not found matches

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

And BTW don't use the legacy implicit join syntax anymore. Use explicit joins.

于 2016-06-11T16:23:39.410 回答
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

Write a wrapper query above your query to check count column

于 2016-06-11T16:23:50.607 回答
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

or if count returns 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
于 2016-06-11T17:34:37.520 回答