gpt4 book ai didi

mysql - 给定表有 3000 万条记录,如何检查 id 是否存在于另一个表中?

转载 作者:行者123 更新时间:2023-11-29 02:30:00 24 4
gpt4 key购买 nike

我知道这个问题似乎重复,但我不知道如何提出不同的问题。

我在 MySQL 数据库中有两个非常简单的表,第一个是表 Users

id,   user_id
1 1
2 3
4 4

第二个是 table 友

id,    user_id,    friend_id
1 1 3
2 1 4
3 1 8

我从要清理的 CSV 文件中转储了数据。我还需要检查表 1 中是否存在 friend_id。第一个表有大约 30000 行,但第二个表有大约 3000 万行。

我用这个查询来检查


SELECT u.user_id, uf.friend_id as exists_friend_ids
FROM Users u, Friends uf
WHERE u.user_id = '1'
and uf.friend_id IN (select user_id from eventify.Users)

However, my desired output would be this but as I cannot run the above query to actually give my test results I cannot continue.

user_id,   exists_friend_ids
1 3
1 4

您可以看到 8 不存在,因为它不存在于 Users 表中。但由于第二张表有超过 3000 万条记录,它一直在我的电脑上运行。我这样做是对的还是这是唯一的方法。还是我应该学习 Hadoop?

我已更新我的查询以使用相等连接。

最佳答案

您是否尝试过使用 GROUP BY friend_id 进行 LEFT JOIN 查询?如果用户不存在,则不会在结果中添加一行。

关于mysql - 给定表有 3000 万条记录,如何检查 id 是否存在于另一个表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14678317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com