小编a_d*_*ter的帖子

postgres中反加入的最快方法

我有两张桌子。用户和帐户。

账户表:-

id  | accountName | userId
--------------------
1   |    natt     |  1
2   |    kelly    |  2
3   |    john     |  3
Run Code Online (Sandbox Code Playgroud)

用户表:-

id  |    username         | 
--------------------
1   |    natt@xyz.com     |
2   |    kelly@xyz.com    |
3   |    john@xyz.com     |
4   |    randy@xyz.com    |
5   |    jamie@xyz.com    |
Run Code Online (Sandbox Code Playgroud)

预期输出:-

userId  |
---------
4       |
5       |
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,表格idusers行为与表格foreign key中的一样accounts。我想获取每个没有关联的用户account(在我的示例中为 4,5)。我可以通过IN or NOT IN但这不是最快的方法。有人可以告诉我最快的方法吗?我正在使用 postgres。

sql postgresql join

-3
推荐指数
1
解决办法
1651
查看次数

标签 统计

join ×1

postgresql ×1

sql ×1