也许我的标题错了,但我想向您展示我的查询以及我在搜索结果中出现的错误:
查询
select distinct
U.id,
U.first_name,
U.last_name,
C.from_user,
case when C.to_user is null
then 99
else C.to_user
end as to_user,
case when C.status is null
then 99
else C.status
end as connection_type,
case when C2.from_user is null
then 99
else C2.from_user
end as from_user_2,
C2.to_user as to_user_2,
case when C2.status is null
then 99
else C2.status
end as connection_type_2,
( 3959 * acos(
cos(radians(19.3901580))
* cos(radians(L.latitude))
* cos(radians(L.longitude) - radians(-99.1733260))
+ sin(radians(19.3901580))
* sin(radians(L.latitude)))
) AS distance
from users U
left join connections C on C.from_user = U.id
left join connections C2 on C2.to_user = U.id
left join locations L on L.user_id = U.id
where U.id != 10
#group by U.id
having distance < 70
#and (connection_type_2 = 1 or connection_type_2 = 99)
#and from_user_2 != 10
#and (to_user != 10 or connection_type != 3)
#and to_user != 10
order by distance asc
结果
如果我取消注释查询中注释的行,则结果为
用户ID:10
如果 to_user = 10 并且 from_user id connection_type = 3 ,我需要不让用户已经与这种情况相匹配,这意味着, 30 的用户不会被选中。
所以我有这个问题,当然这个搜索功能有很多规则,但这是完成它的最后一步!
修改
对请求的一点解释
规则
得到:
不要:
希望你能帮助我!
答案 0 :(得分:1)
你可以试试这个
select * from (
select id, first_name, last_name, (select 3959 * acos(
cos(radians(19.3901580))
* cos(radians(L.latitude))
* cos(radians(L.longitude) - radians(-99.1733260))
+ sin(radians(19.3901580))
* sin(radians(L.latitude)))
from locations l where l.userid = u.id ) as distance from users u
where
(
id in (select from_user from connections where to_user=10 and status=1)
or (id not in (select from_user from connections where to_user=10) and id not in (select to_user from connections where from_user=10) )
)
and id !=10
and id not in (select to_user from connections where from_user =10 and status=2)
and id not in (select to_user from connections where from_user=10 and status=1)
and id not in (select to_user from connections where from_user=10 and status=3)
and id not in (select from_user from connections where to_user=10 and status=3)
) a
where distance < 10;
它尚未优化,但您需要先获得正确的结果。
添加与当前用户的连接状态
select *,
(select status from connections l where (l.to_user=a.id
and l.from_user = 10
or l.from_user = a.id and l.to_user=10
limit 1) as status
from (
select id, first_name, last_name, (select 3959 * acos(
cos(radians(19.3901580))
* cos(radians(L.latitude))
* cos(radians(L.longitude) - radians(-99.1733260))
+ sin(radians(19.3901580))
* sin(radians(L.latitude)))
from locations l where l.userid = u.id ) as distance from users u
where
(
id in (select from_user from connections where to_user=10 and status=1)
or (id not in (select from_user from connections where to_user=10) and id not in (select to_user from connections where from_user=10) )
)
and id !=10
and id not in (select to_user from connections where from_user =10 and status in (1,2,3))
and id not in (select from_user from connections where to_user=10 and status=3)
) a
where distance < 10