IFNULL()可以使用MySQL返回列值为int的字符串吗?

时间:2016-06-11 16:18:02

标签: mysql ifnull

我的查询

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

等,现在我想要它的价值' Count'未被发现"如果值为null或0,那可能吗?我在结果中需要这样的东西:

    Name        Count
    Name1         2
    Name2         3
    Name3    "Not found"

3 个答案:

答案 0 :(得分: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不再使用遗留隐式连接语法。使用显式连接。

答案 1 :(得分: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

在查询上方写一个包装器查询以检查计数列

答案 2 :(得分: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

或者如果count返回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