如何加入这两个加入查询?

时间:2013-10-16 00:59:34

标签: mysql inner-join

有人可以帮我加入这两个加入查询吗?我尝试将第二个查询的ON和WHERE语句放入第一个查询但是没有给出我正在查看的结果。以下是2个查询,它们为我提供了我正在寻找的结果,但我想将它们结合起来。

SELECT username FROM Login 
INNER JOIN Friends 
ON Login.id = Friends.userid2 
WHERE Friends.userid1 = 41


SELECT username FROM Login
INNER JOIN Friends
ON Login.id = Friends.userid1
WHERE Friends.userid2 = 41

2 个答案:

答案 0 :(得分:1)

SELECT username FROM Login 
INNER JOIN Friends 
ON (Login.id = Friends.userid2 AND Friends.userid1 = 41)
    OR (Login.id = Friends.userid1 AND Friends.userid2 = 41)

答案 1 :(得分:0)

鉴于你的描述:

SELECT username FROM Login 
INNER JOIN Friends 
ON Login.id = Friends.userid2 
WHERE Friends.userid1 = 41
UNION
SELECT username FROM Login
INNER JOIN Friends
ON Login.id = Friends.userid1
WHERE Friends.userid2 = 41