我需要将这两个查询合并为 1:
SELECT user1ID AS friendID FROM friends WHERE user2ID = '$userid'
UNION
SELECT user2ID AS friendID FROM friends WHERE user1ID = '$userid'
然后
SELECT firstName, lastName FROM users WHERE ID = friendID (that we just established)
我猜 MySQL 的UNION
( Manual ) 关键字就是你要找的。
编辑由于编辑的问题:
您将需要一个子查询来执行您要执行的操作(JOIN
可能也可以,但会更复杂):
SELECT firstName, lastName FROM users WHERE ID IN
(
SELECT user1ID AS friendID FROM friends WHERE user2ID = '$userid'
UNION
SELECT user2ID AS friendID FROM friends WHERE user1ID = '$userid'
)
请注意,子查询可能会导致非常大的表和/或复杂查询中的性能问题。在这种情况下,您可能希望将子查询重构为 a JOIN
。