【发布时间】:2015-09-09 19:09:21
【问题描述】:
也许我的标题是错误的,但我想向您展示我的查询以及执行搜索的结果中有什么错误:
查询
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 我需要不获取已经匹配的用户在这种情况下,这意味着ID为30的用户将不会被选中。
所以我有这个问题,这个搜索功能当然有很多规则,但这是完成它的最后一步!
编辑:
请求的一点解释
- 连接的用户是他们之间的状态为 3 时
- 传入请求是当它们之间的状态为 1 时,例如:
- from_user:20 to_user:10 status:1(用户 10 有来自 20 的传入请求
- from_user:10 to_user:20 status:1(用户20有来自10的请求
- 当它们之间的状态为 1 时,发出的请求是:
- from_user:20 to_user:10 status:1(用户10已向20发送请求
- from_user:10 to_user:20 status:1(用户20已向10发送请求
- 当状态 from_user to_user 为 2 时,被阻止的用户:
- from_user:20 to_user:30 status:2(用户20屏蔽了用户30)
规则
获取:
- 我需要获取没有任何请求的用户,这意味着用户不在连接表中
- 获取已向当前用户(本例中为 10 个)发送请求的用户。
没有得到:
- 不要让我已经向他们发送邀请的用户
- 不要阻止用户
希望你能帮助我!
【问题讨论】:
-
您的标准有点复杂。你能说清楚吗?
-
@Tim3880 我知道,该查询有 5 条规则,让我尝试在帖子中解释它们。
-
@Tim3880 请检查我编辑的帖子
-
您的标准还远未明确。不要让用户被阻止(谁?任何或#10?);不要让我(我是谁?#10)已经向他们发送邀请的用户。不在连接表中的用户并向当前用户(在本例中为 10 个)发送请求,如何?用户20屏蔽了用户30(#20是发件人,为什么#30被屏蔽了?)
-
我以#10 为例,因为当前用户 id 是登录在试图搜索用户的应用程序中的用户。 --- 我是 #10(示例 id)--- 所有的可能性都在请求的解释中......很难说清楚,因为查询实际上很困难。 --- #30 被阻止,因为 #20 已经阻止了这个 #30 id