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))
我想做一些这样的工作,但这个查询显示错误。如果我在没有 backets 的情况下独立运行这个子查询,它工作正常。 以及如何重写此查询以提高性能
最佳答案
使用 UNION
跳过 IN
,改为执行 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)))
关于mysql - sql子查询中如何使用Union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42551934/