mysql选择子查询在哪里关闭

时间:2016-09-26 08:51:39

标签: mysql sql

用户有多个位置,我想选择至少有一个共同位置的所有用户。

f.e

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没有这样的功能

2 个答案:

答案 0 :(得分:1)

试试这个

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
)

答案 1 :(得分:0)

也许这会奏效,请试试。

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 
    )
)