我有两张桌子,sfuser和会员资格
sfuser得到139,会员桌得到50
我需要查询来挑选谁不是任何项目的成员,这意味着89名成员
这是我的查询
select sfuser.id,sfuser.username,sfuser.email from sfuser INNER JOIN projectmembership ON sfuser.id = projectmembership.member_id ;
但是我得到了50名成员我想要反过来
请小心点亮
最诚挚的问候 Sathish所在
答案 0 :(得分:3)
select
sfuser.id,
sfuser.username,
sfuser.email
from sfuser
LEFT JOIN projectmembership ON sfuser.id = projectmembership.member_id
WHERE
projectmembership.member_id is NULL
它应该返回项目成员表中没有的所有用户。
答案 1 :(得分:1)
INNER JOIN将只生成与两个表匹配的行。您可以执行左连接并过滤掉空白记录。类似的东西:
SELECT sfuser.id,
sfuser.username,
sfuser.email
FROM sfuser LEFT JOIN projectmembership ON sfuser.id = projectmembership.member_id
WHERE (projectmembership.member_id is null);
答案 2 :(得分:1)
内部联接将返回在两个表之间共享完成连接的值的所有行。 (见this chart)
您要查找的是左连接,即左表中右侧没有匹配的所有行。
select sfuser.id,
sfuser.username,
sfuser.email
from sfuser
LEFT JOIN projectmembership
ON sfuser.id = projectmembership.member_id
答案 3 :(得分:1)
如果sfuser记录在项目成员资格表中可能有多次出现,那么就忘记外连接 - NOT EXISTS就是你想要的:
select sid,
username,
email
from sfuser
where not exists (
select null
from projectmembership
where sfuser.id = projectmembership.member_id);