问题时的MYSQL案例

时间:2010-03-30 17:52:16

标签: mysql case

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

如何使内部连接像profile.id = friendships。(这里将选择所需的一个键),但它不起作用。请帮助:P

它不能:

         `profiles`.id =  (CASE WHEN friendships.profile_id = 1
         THEN `friendships`.`friend_id` ELSE `friendships`.`profile_id` END)

2 个答案:

答案 0 :(得分:1)

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)

答案 1 :(得分:0)

怎么样

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))