tags:

views:

44

answers:

2
    SELECT `profiles`.* 
    FROM `profiles` 
    INNER JOIN `friendships` 
    ON `profiles`.id = `friendships`.(CASE WHEN friendships.profile_id = 1
    THEN`friend_id` ELSE `profile_id` END)

How can i make the inner join like profile.id = friendships.(here will select the one key that is needed) but it doesnt work. please help :P

it cant be:

         `profiles`.id =  (CASE WHEN friendships.profile_id = 1
         THEN `friendships`.`friend_id` ELSE `friendships`.`profile_id` END)
+1  A: 
SELECT `profiles`.* 
FROM `profiles` 
INNER JOIN `friendships` 
ON `profiles`.id = (CASE WHEN friendships.profile_id = 1
THEN friendships.`friend_id` ELSE friendships.`profile_id` END)
Andrey
the problem here is that i use rails and i use the :foreign_key => 'case...' and i cant use the friendships.`profile_id`, because then i got: friendships.friendships.`profile_id` and is not workinganyway thanks
Totty
sorry, i am not familiar with rails
Andrey
A: 

How about

SELECT  `profiles`.* 
  FROM `profiles` 
  INNER JOIN `friendships`
   ON (profiles.id = friendships.profile_id AND NOT (profile_id = 1))
       OR ((profiles_id =friendships.friend_id) AND (profile_id = 1))
Loopo
i cant. rails already generates all the code:... profiles.id = frienships.{only here may i add any code}....
Totty
Then I think you'll have to get rails to generate a different query, one that allows multiple conditions on the JOIN. I have no clue about rails though, so I can't help you there.
Loopo