gpt4 book ai didi

mysql - 检查两个表之间存在和不存在的值

转载 作者:行者123 更新时间:2023-11-29 01:26:36 25 4
gpt4 key购买 nike

我有 3 个表,一个是帐户,一个是 friend ,另一个是消费者。像这样:

table_accounts

id | account_name

table_friends

id | account_id | people_id

table_consumers

id | account_id | people_id

我需要交叉以下信息:

哪个consumer_id同时存在于两个表中,简单的像这样:

SELECT 
*
FROM
table_friends,
table_consumers
WHERE
table_friend.account_id = 12345
AND table_friend.account_id = table_consumers.account_id
GROUP BY table_friend.people_id

这个查询很慢

好吧,我现在需要获取 consumer_id 的 friend 表,它们不在 consumers 表中。在第三时刻,找出 friends 表中不存在的 consumer_id。但我认为这是同一回事......

我的疑惑是关于逻辑的,我想不出如何跨越这些信息。

最佳答案

这可能或多或少是您尝试做的事情:(并查看 Subqueries with EXISTS vs IN - MySQL)

SELECT *
FROM table_friends
WHERE
NOT EXISTS (
SELECT *
FROM table_consumers
WHERE table_consumers.people_id = table_friends.people_id
)

顺便说一句,你说“这个查询很慢”你查询了多少行?什么是“慢”?你有一些你需要的索引吗?

关于mysql - 检查两个表之间存在和不存在的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356768/

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