MYSQL加入第3表

时间:2012-01-18 21:34:58

标签: mysql sql

我有以下MYSQL

SELECT T2.name as username, T2.id as userid, 
       T2.lastactivity as lastactivity, T2.photo as avatar
FROM exchange AS T2
  INNER JOIN 
    ( SELECT (CASE WHEN `buddy`.`penpalA` = 887 
                     THEN `buddy`.`penpalB` 
                     ELSE `buddy`.`penpalA` 
              END) AS 'friend'
      FROM `buddy`
      WHERE status = 1 AND `penpalA` = 887
         OR `penpalB` = 887
    ) AS _Temp 
    ON _Temp.friend = T2.`id`

如何添加另一个表以获取__Temp.friend和现在T3.userid是公分母的其他详细信息

1 个答案:

答案 0 :(得分:1)

试试这个:

   SELECT T2.name as username, T2.id as userid, 
           T2.lastactivity as lastactivity, T2.photo as avatar,T3.*
    FROM exchange AS T2
      INNER JOIN 
        ( SELECT (CASE WHEN `buddy`.`penpalA` = 887 
                         THEN `buddy`.`penpalB` 
                         ELSE `buddy`.`penpalA` 
                  END) AS 'friend'
          FROM `buddy`
          WHERE status = 1 AND `penpalA` = 887
             OR `penpalB` = 887
        ) AS _Temp 
        ON _Temp.friend = T2.`id`
     LEFT JOIN details T3 ON _Temp.friend=T3.UserId