mysql - find `accounts` with whom user haven't yet made friendship -


i have table accounts (id, full_name) , account_friends (account_id, friend_id). friend_id account id of user. how find accounts whom account id=1 haven't made friendship yet?

in response @omg ponies.

basically, understand require multiple joins. logic goes this:

select     a2.id     accounts a1  /* left/right join a1 account_friends */  inner join     accounts a2 on     af1.friend_id = a2.id     a1.id; 

though don't understand left/right join bit should trick of find not-friend entries.

this give account ids , names id not account id=1 , not friend of account id=1:

select id, full_name   accounts  id not in (select friend_id                     account_friends                    account_id = 1)    , id <> 1 

as antonis_wrx mentioned in comments, above query hold true if friendships account stored account->friend (i.e. (1,2)) , not mixture i.e. (1,2) , (3,1).

here query work second case:

select id, full_name   accounts  id not in (select friend_id account_friends account_id = 1)    , id not in (select account_id account_friends friend_id = 1)    , id <> 1 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -