Skip to content

Instantly share code, notes, and snippets.

@tobikris
Created June 11, 2015 13:02
Show Gist options
  • Save tobikris/c47dc5eba9b57ea6d334 to your computer and use it in GitHub Desktop.
Save tobikris/c47dc5eba9b57ea6d334 to your computer and use it in GitHub Desktop.
SQL SELECT for bi-directional friends
SELECT
IF(`user_id`="1",`user_id`,`friend_id`) AS `user_id`,
IF(`user_id`="1",`friend_id`,`user_id`) AS `friend_id`,
`user_id` AS `source_id`
FROM
friends
WHERE
`user_id`="1"
OR
`friend_id`="1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment