我有两张桌子。用户和帐户。
账户表:-
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)
正如您所看到的,表格id的users行为与表格foreign key中的一样accounts。我想获取每个没有关联的用户account(在我的示例中为 4,5)。我可以通过IN or NOT IN但这不是最快的方法。有人可以告诉我最快的方法吗?我正在使用 postgres。