I got two tables,sfuser and membership
sfuser got 139 and membership table got 50
I need the query to pick who are not a member of any project which means 89 members
Here is my query
select sfuser.id,sfuser.username,sfuser.email from sfuser INNER JOIN projectmembership ON sfuser.id = projectmembership.member_id ;
But am getting the 50 users who are members i want the other way around
Kindly throw some light
Best Regards Sathish
select
sfuser.id,
sfuser.username,
sfuser.email
from sfuser
LEFT JOIN projectmembership ON sfuser.id = projectmembership.member_id
WHERE
projectmembership.member_id is NULL
It should return all users that isn't in projectmembership table.
INNER JOIN will only produce rows that matches both tables.. You can do a left join and filter out the blank records. something like:
SELECT sfuser.id,
sfuser.username,
sfuser.email
FROM sfuser LEFT JOIN projectmembership ON sfuser.id = projectmembership.member_id
WHERE (projectmembership.member_id is null);
An inner join will return all rows where the values the join is done on are shared between the two tables. (See this chart )
What you are looking for is a left join, that is, all rows in the left table that have no match in the right.
select sfuser.id,
sfuser.username,
sfuser.email
from sfuser
LEFT JOIN projectmembership
ON sfuser.id = projectmembership.member_id
If sfuser records can have multiple occurrences in the project membership table then forget the outer join -- NOT EXISTS is what you want:
select sid,
username,
email
from sfuser
where not exists (
select null
from projectmembership
where sfuser.id = projectmembership.member_id);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.