代码之家  ›  专栏  ›  技术社区  ›  Potato

如果被阻止,不要包含在通配符搜索中

  •  0
  • Potato  · 技术社区  · 7 年前

    我这里有个问题,

    有两个用户

    用户1配置文件\u id

    3115267284
    

    3028630039
    

    有一个名为friends的表,其结构为

    from_profile_id
    to_profile_id
    request
    blocked_by
    

    var_id  profile_id  fullname             firstname   lastname
    180     3115267284  John Louis Domincel  John Louis  Domincel  
    181     3028630039  James Domincel       James       Domincel  
    182     3450766778  asdasd asdsadas      asdasd      asdsadas
    

    假设Friends表的数据是

    from_profile_id  to_profile_id  request  blocked_by
    3028630039       3115267284     3        3115267284     
    

    SELECT * FROM users 
    WHERE fullname LIKE "%'.$s_final.'%" LIMIT 5
    

    enter image description here

    我只想使配置文件2,这是3028630039将无法搜索配置文件1,这是3115267284原因配置文件id 1是谁阻止了两个,但配置文件1将能够搜索配置文件id 2

    我也试过使用 JOIN 和 ISNOT() 但没有成功

    2 回复  |  直到 7 年前
        1
  •  1
  •   Madhur Bhaiya    7 年前

    $searcher_profile_id .

    Left Join WHERE .. IS NOT NULL . 这有时被称为“反连接”:

    SELECT u.* 
    FROM users AS u 
    LEFT JOIN friends AS f 
      ON f.blocked_by = u.profile_id AND 
         f.blocked_by <> "' . (int)$searcher_profile_id . '" AND 
         (
          f.to_profile_id = "' . (int)$searcher_profile_id . '" OR 
          f.from_profile_id = "' . (int)$searcher_profile_id . '"
         ) AND 
         f.request = 3
    WHERE u.fullname LIKE "%'.$s_final.'%" AND 
          f.from_profile_id IS NULL 
    LIMIT 5
    
        2
  •  1
  •   Sean Leroy    7 年前

    假设有人被多人阻止,我会在php代码中查询SQL数据库,并为运行搜索的用户提取所有被阻止的id。然后,我将遍历并检查php代码,如果所查询的ID在列表中,则停止该过程。

    如果你真的在寻找效率,这可能不是最好的解决方案。我想您也可以运行查询,然后在php中执行检查,这样就不会执行2个查询。

    推荐文章