gpt4 book ai didi

MySQL查询四张表

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

我有 4 个表,分别是 A、B、C 和 D。

每个表都有一个公共(public)字段,那就是电子邮件。

现在我想要 B、C 或 D 中存在但 A 中不存在的所有电子邮件。

最佳答案

看看这个 fiddle :http://sqlfiddle.com/#!9/f35dc/1

我能想到的最好方法是使用 3 个单独的查询,每个查询都左连接到表 A。使用 UNION DISTINCT 将为您提供表 B、C 和 D 中不存在于表 A 中的唯一电子邮件值.这是我使用的查询:

SELECT
b.email
FROM
b
LEFT JOIN a ON b.email = a.email
WHERE a.email IS NULL
UNION DISTINCT
SELECT
c.email
FROM
c
LEFT JOIN a ON c.email = a.email
WHERE a.email IS NULL
UNION DISTINCT
SELECT
d.email
FROM
d
LEFT JOIN a ON d.email = a.email
WHERE a.email IS NULL

另一个答案建议使用子选择,但我在 mySQL 中使用子选择的经验是它们的性能不是很好。

关于MySQL查询四张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34926115/

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