mysql选择与子查询关闭
问题描述:
用户有多个位置,我想选择至少有一个共同的位置,以验证用户的所有用户。mysql选择与子查询关闭
FE
select * from users as user where
(select location_id from user_locations where user_id = auth()->user->id)
'has common operator'
(select location_id from user_locations where user_id = user.id)
我想比较两个数组,如果他们有共同的元素,但我认为MySQL有没有这样的功能
答
试试这个
SELECT u.*
FROM users u
LEFT JOIN user_locations ul ON ul. user_id = u.id
WHERE ul.location_id IN (
SELECT location_id
FROM user_locations
WHERE user_id = auth()->user->id
)
答
也许这将工作,请尝试。
select *
from users as user
where
exists (
select 1
from user_locations ul
where ul.user_id = user.id
and exists (
select 1
from user_locations ul_in
where ul_in.user_id = auth()->user->id
and ul_in.location_id = ul.location_id
)
)
这不会没有选择不同的工作,COS会选择相同的用户多次,因为他们有共同的location_id。这也不是最佳方式,也不想使用连接。 – Doodles
我想比较数组,如果他们有共同的元素,但我认为MySQL没有这样的功能 – Doodles
LEFT JOIN作为一个内部联接执行。 – jarlh