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"
halfer
  • 19,824
  • 17
  • 99
  • 186
Gilzy
  • 103
  • 1
  • 1
  • 4

3 Answers3

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.

juergen d
  • 201,996
  • 37
  • 293
  • 362
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

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
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
PawelN
  • 339
  • 2
  • 9