请查看此查询,并尝试向我提供任何其他可以更有效地提供完全相同结果的提示。
SELECT username
FROM users
WHERE username NOT IN (
SELECT DISTINCT username
FROM users, friends
WHERE 'user1' IN (you,friend,username)
AND you IN ('user1',username)
AND friend IN ('user1',username))
AND username <> 'user1'
答案 0 :(得分:0)
当然效率不高 - 你的子选择中有笛卡尔。从您的查询中不是特别清楚您实际想要实现的目标。如果你描述了这个问题,它将会更有帮助。尽管如此,我猜你正试图找到所有与'user1'不是朋友的用户..?在这种情况下,这应该有效:
SELECT username
FROM users
WHERE username NOT IN (
SELECT DISTINCT you
FROM friends
WHERE friend = 'user1'
UNION ALL
SELECT DISTINCT friend
FROM friends
WHERE you = 'user1')
AND username <> 'user1'