This question already has an answer here:
Table 1:
userid name
1 Dharam
2 James
3 Ramesh
4 David
Table 2:
follower(id) following(id)
1 2
2 1
2 3
3 4
Here, I have Table 1 for all users and Table 2 for who is following whom.
I want to get result as
Show the users from 'Table 1' WHERE 'Table 1' users should not be already following the same person
Means, Get users whom user 1 is not following FROM Table 1 filtered by Table 2
Result:
3
4
</div>
Use: inner query
SELECT * from users WHERE user_id NOT IN (
SELECT user_id_foreign_key FROM follow_details)
Or you can do it with a join
:
SELECT t1.userid, t2.follower
FROM tbl1 AS t1
LEFT JOIN tbl2 ON t1.userid = t2.follower
HAVING follower IS NOT NULL