Search code examples
mysqlifnull

Can IFNULL() return a string where the column value is int, using MySQL?


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"

Solution

  •     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