Skip to content

Instantly share code, notes, and snippets.

@santiagobasulto
Created March 30, 2012 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santiagobasulto/2255231 to your computer and use it in GitHub Desktop.
Save santiagobasulto/2255231 to your computer and use it in GitHub Desktop.
SELECT u.id,u.first_name FROM auth_user u
JOIN (
SELECT IF(user1 = 1,Null,user1) res1 ,IF(user2 = 1,Null,user2) res2
FROM auth_user u JOIN friendship f
ON (u.id = f.user1 OR u.id=f.user2)
WHERE
u.id = 1
) t ON (u.id=res1 OR u.id=res2)
-- Dustin QUery
select * from auth_user u
left join (
select user1_id as friend_id from profile_friendship where user2_id = 1 and status = 'Accepted'
union
select user2_id as friend_id from profile_friendship where user1_id = 1 and status = 'Accepted'
) as f on f.friend_id = u.id
where u.first_name like 'd%'
order by f.friend_id > -1 desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment