MySQL:不要选择其他加入

时间:2015-06-23 19:41:54

标签: mysql sql

也许我的标题错了,但我想向您展示我的查询以及我在搜索结果中出现的错误:

查询

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

结果

enter image description here

如果我取消注释查询中注释的行,则结果为

enter image description here

用户ID:10

如果 to_user = 10 并且 from_user id connection_type = 3 ,我需要不让用户已经与这种情况相匹配,这意味着, 30 的用户不会被选中。

所以我有这个问题,当然这个搜索功能有很多规则,但这是完成它的最后一步!

修改

对请求的一点解释

  • 连接的用户是指他们之间的状态为3
  • 传入请求是指它们之间的状态为1,如:
    • from_user:20 to_user:10状态:1(用户10有来自20的传入请求
    • from_user:10 to_user:20状态:1(用户20有来自10的传入请求
  • 传出请求是指它们之间的状态为1,如:
    • from_user:20 to_user:10状态:1(用户10已向20发送请求
    • from_user:10 to_user:20状态:1(用户20已向10发送请求
  • 阻止用户是指from_user to_user为2时的状态:
    • from_user:20 to_user:30状态:2(用户20阻止用户30)

规则

得到:

  • 我需要获得没有任何请求的用户,这意味着不在连接表中的用户
  • 获取已发送给当前用户(在本例中为10)的用户请求。

不要:

  • 不要让我已向他们发送邀请的用户
  • 不要让用户被阻止

希望你能帮助我!

1 个答案:

答案 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