【问题标题】:MySQL: Don't select records matched in other joinMySQL:不要选择在其他连接中匹配的记录
【发布时间】: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 = 10from_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

标签: mysql sql


【解决方案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

【讨论】:

  • 前三个不在的可以只转换成一个and id not in (select to_user from connections where from_user =10 and status in(1,2,3))
  • @JorgeCampos 这是真的!
  • 您好,请您帮我从连接表中选择“状态”字段。
  • 您可以将上述查询的结果和您的连接表连接起来以包含状态。实际密钥取决于您需要的状态。
  • 使用连接我必须两次连接到“from_user”和“to_user”的表连接,我只想为当前连接表中的用户获取“状态”用户(10),如果不是很好,则显示为空。并且使用 join 我遇到了与原始帖子相同的问题,即返回更多行与同一用户但与其他用户连接而不是当前 (10)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 2012-01-06
  • 2015-06-01
相关资源
最近更新 更多