select user_id,name, login,lastseen from users where user_id
in((select friend_id from connections where user_id=1 and connection=1)
union (select user_id from connections where friend_id=1 and connection=1))
我想做一些像这样的工作,但这个查询显示错误。如果我在没有支持的情况下独立运行这个子查询,它工作正常。 以及如何重写此查询以增加性能
答案 0 :(得分:2)
使用IN
跳过UNION
,改为EXISTS
:
select user_id, name, login, lastseen
from users u
where exists (select 1 from connections c
where c.connection = 1
and ((u.user_id = c.friend_id and c.user_id = 1) or
(c.user_id = u.user_id and c.friend_id = 1)))
答案 1 :(得分:1)
删除额外的括号
select user_id,name, login,lastseen
from users
where user_id
in (
select friend_id
from connections
where user_id=1 and connection=1
union
select user_id
from connections
where friend_id=1 and connection=1
)