检查两个表中是否存在一行

nxowjjhe  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(240)

这个问题在这里已经有答案了

如何检查两个不同表中是否存在两列数据?mysql(2个答案)
两年前关门了。
我有两张table:
朋友:

accountId | friendId
--------------------
       A1 |      B15
       A1 |      C34
       A1 |      D48

和追随者

accountId | followerId
----------------------
       A1 |        B15
       A1 |        C34

我尝试运行一个mysql查询,它将返回一个“friendid”,而这个“friendid”不存在于另一个表的“followerid”列中。基本上,我试图找到,哪些行从“朋友”表,不存在于“追随者”表。
我试过:

SELECT *
FROM friends AS f
LEFT JOIN followers AS l
ON f.accountId = l.accountId
WHERE l.accountId IS NULL

但它什么也不回。没有结果。只是空的。我已经手动检查了表的有效性,它确实包含这样的条目。有什么想法吗?

mzaanser

mzaanser1#

您可以使用:

SELECT *
FROM friends AS f
LEFT JOIN followers AS l
  ON f.accountId = l.accountId
 AND f.friend_id = l.follower_id   -- additional condition
WHERE l.accountId IS NULL;

输出:

accountId | friendId | accountId | follower_id
       A1 |      D48 | NULL      | NULL

相关问题