我遇到查询问题,有时会返回一个空字段。我曾尝试使用CASE和COALESCE,但我可能错误地构造了查询。
如果b.user_id返回空,我需要在结果中执行的操作是将a.useracctid中的值复制到b.user_id中。我希望这是有道理的???
这是我的查询,无论我为我尝试过哪些错误。
感谢您提供的任何想法或提示
SELECT b.user_id,a.responsetime,a.latitude,a.longitude,
a.status,a.updatetime,c.address
FROM callouts_response a
LEFT JOIN ldap_user_accounts b on a.useracctid = b.id
LEFT JOIN callouts c on a.calloutid = c.id
WHERE calloutid = :cid
答案 0 :(得分:0)
试试这个:
SELECT IF(b.user_id IS NULL, a.useracctid, b.user_id), a.responsetime,a.latitude,a.longitude,
a.status,a.updatetime,c.address
FROM callouts_response a
LEFT JOIN ldap_user_accounts b on a.useracctid = b.id
LEFT JOIN callouts c on a.calloutid = c.id
WHERE calloutid = :cid
答案 1 :(得分:0)
不确定您如何使用COALESCE()
,但您可以这样做
SELECT COALESCE(b.user_id,a.useracctid) as somecol,
a.responsetime,
a.latitude,
a.longitude,
a.status,
a.updatetime,
c.address
FROM callouts_response a
LEFT JOIN ldap_user_accounts b on a.useracctid = b.id
LEFT JOIN callouts c on a.calloutid = c.id
WHERE a.calloutid = :cid