mysql - SQL Union or other table join -


i'm having trouble wrapping head around sql query, trying figure out something.

just quickly, here (simple) table: it's representation of friends added in social network site. have use table format this, user1 , user2 both primary keys.

          user1       |       user2     --------------------------------------       foo@a.com       |   things@stuff.com       foo@a.com       |   lala@hi.com       things@stuff.com|   lol@k.com       lol@k.com       |   foo@a.com       foo@a.com       |   lol@k.com 

what need write select statement return unique users friends foo@a.com example.

select user2 members  user1 = 'things@stuff.com' 

would return lol@k.com , not foo@a.com, though need return latter.

select * members user1 = 'foo@a.com' or user2 = 'foo@a.com' 

would return error think? selecting multiple columns probably.

so figure union join or other join necessary (union no duplicates?) i'm not sure how go doing it.

i'm trying implement in php, if last query wrote worked, i'm not sure how echo user1 or user2 depending on returned/which 1 needed if makes sense.

using union (for performance) :-

select user2 members user1 = 'foo@a.com' union select user1 members user2 = 'foo@a.com' 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -