gpt4 book ai didi

mysql - 具有两个不同字段的两个条件的 case 语句

转载 作者:可可西里 更新时间:2023-11-01 08:47:26 26 4
gpt4 key购买 nike

这是我的场景...我想检查该成员是否在一张 table 上有手机,如果是,则显示他的手机..否则检查他是否有另一张 table 的固定电话...如果是,则显示固定电话...否则为空白。不确定我们是否可以使用 case 语句来比较两个不同的字段

SELECT a.name as name, b.name as parent_name, a.mobile, b.phone, 
case
when a.mobile is not null or a.mobile<>'' then a.mobile
else b.phone
end
as phone
FROM family_member a join family_header b where a.name='sam' and a.id=b.match_key;

请指导我.. 仅执行第一个 case 语句并在可用时显示 a.mobile.. 但如果手机不可用,则不显示固定电话..

最佳答案

如果 mobile 是一个空字符串,它是 not null,所以这个条件匹配所有那些 a.mobile 是空的记录字符串。所以我的猜测是,您的空白字段都不是 null

将条件更改为:

when a.mobile is not null AND a.mobile<>'' then

也许更好:使该字段不可为空,因此它始终包含电话号码或空字符串。那么你可以将条件简化为:

when a.mobile<>'' then

关于mysql - 具有两个不同字段的两个条件的 case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24333717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com