如何使用排除进行 SQL JOIN

帕维尔·萨尔波夫

我有3张桌子:

  1. 用户 [id, name]
  2. 黑名单 [blockerid,blockedid]
  3. 位置 [uuid,纬度,经度]

我需要从位置表中获取所有数据,条件是如果请求位置的人在黑名单中显示为阻止者或被阻止,则不会看到这些禁止的位置。例如:

Blacklist:
10 11

Location
10 74.1231 51.12312
11 82.1231 -1.31241
12 10.2121 34.12312

如果 12 询问位置,他会得到所有。如果 11 询问位置,他只会得到 12 的位置,与 10 相同。

在请求中需要帮助

scaisEdge

您可以使用联合来获取 blockerid 和 blocksid 作为 not in 的 id

  select * 
  from localtion 
  where uuid not in ( 
        select blockerid as my_id
        from (
            select  blockerid,  blockedid
            from  Blacklist
            where   blockerid = your_id
            or   blockedid = your_id 
        ) AS alias
        union 
        select blockedid
        from (
            select  blockerid,  blockedid
            from  Blacklist
            where   blockerid = your_id
            or   blockedid = your_id 
        ) AS alias

  )

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章