在此mysql查询中添加JOIN

时间:2012-07-30 10:11:51

标签: mysql join

SELECT t1.* FROM HOSTS AS t1
WHERE
t1.BOSS = 'BRUCE'
OR ( t1.BOSS_TYPE = 'manager'
 AND t1.BOSS_ID = 'BRUCE' )
OR t1.BOSS IN ( SELECT t2.STORES FROM
  FOLLOW_STORES AS t2 
 WHERE t2.BOSSES = 'BRUCE' )

以上查询效果很好,但我想添加一个JOIN来获取额外信息

我想添加此JOIN,但我无法让它工作。我必须将它插入错误的地方。

 JOIN MANAGER_POSTS AS t3 ON ( t1.POSTER_ID = t3.POSTER_ID
    AND t1.SUBMISSION_TYPE =  'post' )

添加联接以获取此额外信息的正确方法是什么

2 个答案:

答案 0 :(得分:1)

在这里试试这个:

SELECT  t1.* , t3.*
FROM    HOSTS AS t1 
            INNER JOIN MANAGER_POSTS AS t3 
                ON ( t1.POSTER_ID = t3.POSTER_ID AND 
                     t1.SUBMISSION_TYPE =  'post' )
WHERE   t1.BOSS = 'BRUCE' 
        OR (t1.BOSS_TYPE = 'manager' AND t1.BOSS_ID = 'BRUCE' )
        OR t1.BOSS IN   ( 
                            SELECT t2.STORES 
                            FROM FOLLOW_STORES AS t2 
                            WHERE t2.BOSSES = 'BRUCE' 
                        )

答案 1 :(得分:0)

您需要在JOIN之后和FROM Hosts

之前添加WHERE条款
SELECT t1.* 
FROM HOSTS AS t1
INNER JOIN MANAGER_POSTS AS t3 
    ON ( t1.POSTER_ID = t3.POSTER_ID
    AND t1.SUBMISSION_TYPE =  'post' )
WHERE t1.BOSS = 'BRUCE'
    OR ( t1.BOSS_TYPE = 'manager'
          AND t1.BOSS_ID = 'BRUCE' )
    OR t1.BOSS IN ( SELECT t2.STORES 
                    FROM FOLLOW_STORES AS t2 
                    WHERE t2.BOSSES = 'BRUCE' )