join - MySQL - Select in same table -
i need use mysql
show users (a) not follow b, (a) followed b. gonna used suggest connections on social media website.
my table structure:
id followed follower
i've tried use left join
or not exists
, didn't work.
@drapp shows me how user left join
on query. works. now, how add users information on query (left join
+ inner join
).
inner join need follow table , user table (inner join users on follow.follower = user.id
)
users`s table structure:
id name
given scenario of sample data of "yourtable"
id follower followed 1 c 2 b c 3 b d 4 b e 5 c b 6 d
the people followed "b" include "c", "d" , "e" users. that, user "c" follows "b". so, looking no match a/b, b/a type of combination. basis of left-join. clause querying records based on user "b" considering originating "follower".
select yt.followed, u.name yourtable yt left join yourtable yt2 on yt.followed = yt2.follower , yt.follower = yt2.followed join users u on yt.followed = u.id yt.follower = 'b' , yt2.followed null
to name, notice join original table based on user "b", not finding corrresponding inverse match. so, joining on original record's followed (who "b" following) user table same id in user table. grab user's name query.
Comments
Post a Comment