0

谁能帮我加入这两个加入查询?我尝试将第二个查询的 ON 和 WHERE 语句放入第一个查询中,但这并没有给我正在寻找的结果。下面是两个查询,它们给了我正在寻找的结果,但我想将它们结合起来。

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
4

2 回答 2

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)
于 2013-10-16T01:05:53.247 回答
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
于 2013-10-16T01:45:16.913 回答