左连接返回所有结果,即使条件不满足
Left join returning all the results even when the condition is not satisfied
我有一个简单的查询
select *
from employees as e
left join email_notification as en
on e.id=en.eid and en.`status` != 'SUCCESS';
现在我在 email_notification table 中有一个记录
状态为成功,但仍然从员工 table 获取该记录。
我猜你的意思是:
select *
from employees as e
left join email_notification as en
on e.id=en.eid
where en.`status` != 'SUCCESS';
我的意思是,status
的检查不应与 join
一起进行,而是参与最终结果。
您需要将加入员工加入 "successes" 并选择那些没有加入的员工,如下所示:
SELECT e.*
FROM employees AS e
LEFT email_notification AS en
ON e.id=en.eid and en.`status` = 'SUCCESS'
WHERE en.id IS NULL
;
为什么不尝试使用 IN
?
select *
from employees as e
where id not in(
select eid from email_notification
where status != 'SUCCESS'
)
我有一个简单的查询
select *
from employees as e
left join email_notification as en
on e.id=en.eid and en.`status` != 'SUCCESS';
现在我在 email_notification table 中有一个记录 状态为成功,但仍然从员工 table 获取该记录。
我猜你的意思是:
select *
from employees as e
left join email_notification as en
on e.id=en.eid
where en.`status` != 'SUCCESS';
我的意思是,status
的检查不应与 join
一起进行,而是参与最终结果。
您需要将加入员工加入 "successes" 并选择那些没有加入的员工,如下所示:
SELECT e.*
FROM employees AS e
LEFT email_notification AS en
ON e.id=en.eid and en.`status` = 'SUCCESS'
WHERE en.id IS NULL
;
为什么不尝试使用 IN
?
select *
from employees as e
where id not in(
select eid from email_notification
where status != 'SUCCESS'
)