gpt4 book ai didi

mysql - 如果第三个表中存在特定匹配,则从 2 个表中选择结果

转载 作者:行者123 更新时间:2023-11-29 20:51:07 24 4
gpt4 key购买 nike

我有三张 table

t1
--------------
userID
userEmail
userName
userType

t2
--------------
businessID
businessUserID

t3
--------------
recordID
recordBusinessID
action (ENUM: pending, open, closed)

如果 t3 中找到的结果仅包含带有 action = 'pending' 的记录,我需要检索记录。

SELECT
t2.businessID,
t1.userEmail,
t1.userName
FROM t2
LEFT JOIN t1 ON (t1.userID = t2.businessUserID)
LEFT JOIN t3 ON (t3.recordBusinessID = t2.businessID)
WHERE userType = 'active'
AND t3.action = 'pending'
t3.action != 'open'
t3.action != 'closed'

看起来我应该得到结果,因为我当前的 t3 是空的,但我没有。我错过了什么?

t3 可以有结果,但我只需要在 t3.action 只是“待处理”时进行匹配。

最佳答案

即使您检查不等于,将 Null 与任何值进行比较也会返回 Null。因此,要测试是否没有对应的值或它不等于某个值,请执行以下操作:

where ... and (t3.action is null or t3.action != 'pending')

关于mysql - 如果第三个表中存在特定匹配,则从 2 个表中选择结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38043775/

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