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

SQL Tinder为已经喜欢您的用户排定优先顺序

  •  0
  • atkayla  · 技术社区  · 7 年前
    profile
    ---
    id  name
    1   John
    2   Jane
    3   Jill
    ...
    
    swipe
    ---
    id  profile_1_id  profile_2_id  liked
    1   2             1             true
    2   3             1             false
    ...
    

    1. 已经喜欢你的用户,你可以立即与之匹配,推到顶部
    2. 其他用户
    3. (这个问题不在讨论范围之内,但也会涉及到一些更具吸引力的用户)

    “先找到喜欢John的人,然后用随机用户填充其他人”的SQL是什么?这是一个WHERE(case if else)还是其他语句?

    2 回复  |  直到 7 年前
        1
  •  2
  •   GMB    7 年前

    这是一个应该满足您需要的查询。

    CASE . 喜欢John will的用户将获得更高的优先级,并将按 id LIMIT 条款

    我在中测试了这个查询 this db fiddle . 你需要替换问号( ? )在 案例 子句,该子句具有要为其生成卡的用户的id( 1 在您的示例数据中为John)。

    SELECT
        p.id, 
        p.name
    FROM 
        profile p
        LEFT JOIN swipe s on s.profile_1_id = p.id
    ORDER BY 
        CASE s.profile_2_id
            WHEN ? THEN 0
            ELSE FLOOR(random() * 10) + 1
        END,
        p.id
    LIMIT 20
    
        2
  •  1
  •   LoztInSpace    7 年前

    你可以试试这样,但我认为你过于简单化了。是否要将不喜欢的人排除在其他人之外?

      select * from profile p
        left outer join swipe s on (p.id=profile_1_id and s.profile_2_id  = 1 and liked = true)
        where 
         p.id<>1
        order by coalesce(profile_2_id , random()*-1000000) desc
        limit 20