我有三张桌子

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只不过是“ pending”,我就需要匹配。

最佳答案

将Null与任何值进行比较都会返回Null,即使您选中not equal to。因此,要测试没有相应的值或它不等于某个值,请执行以下操作:

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

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

10-16 11:57