IFNULL() return 列值为 int 的字符串可以使用 MySQL 吗?
Can IFNULL() return a string where the column value is int, using MySQL?
我的查询
select cname, count(screening_occapancy.idclient) as 'Count'
from client, screening_occapancy
where client.client_no = screening_occapancy.idclient
group by cname
returns 以下:
Name Count
Name1 2
Name2 3
Name3 6
etc,现在我希望它在 'Count' 中的值是 "not found" 如果值为 null 或 0,这可能吗?我的结果中需要这样的东西:
Name Count
Name1 2
Name2 3
Name3 "Not found"
使用 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
顺便说一句,不再使用遗留的隐式连接语法。使用显式连接。
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
在您的查询上方写一个包装器查询以检查计数列
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
或者如果计数 returns 为空?
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
我的查询
select cname, count(screening_occapancy.idclient) as 'Count'
from client, screening_occapancy
where client.client_no = screening_occapancy.idclient
group by cname
returns 以下:
Name Count
Name1 2
Name2 3
Name3 6
etc,现在我希望它在 'Count' 中的值是 "not found" 如果值为 null 或 0,这可能吗?我的结果中需要这样的东西:
Name Count
Name1 2
Name2 3
Name3 "Not found"
使用 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
顺便说一句,不再使用遗留的隐式连接语法。使用显式连接。
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
在您的查询上方写一个包装器查询以检查计数列
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
或者如果计数 returns 为空?
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