gpt4 book ai didi

mysql - 根据多列选择第二个表中不存在的行

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

我正在使用mysql。我有两张 table 。它们不共享 key (但有一些共同的属性)。我必须从表 A 中选择表 B 中不存在的行。基于谷歌搜索和堆栈溢出,我编写了这个查询

select ta.a, ta.b, ta.c from 
(select a, b, c, -1 * c d from TableA) ta
left join TableB tb on (ta.a = tb.a and ta.b = tb.b and ta.d = tb.c)
where tb.a is null and tb.b is null and tb.c is null;

但我不确定这是否正确。你能确认或告诉我我写的是否正确吗?

如果 TableB 有一行的 a 和 b 值相同且 c 值为负值,那么最终我不应该从 TableA 中获取任何行。

最佳答案

假设没有一个键值是NULL,你可以这样做。一次 NULL 比较就足够了:

select ta.a, ta.b, ta.c
from TableA ta left join
TableB tb
on ta.a = tb.a and ta.b = tb.b and - ta.c = tb.c
where tb.a is null;

我不建议使用子查询来定义d。 MySQL 倾向于具体化子查询,这会增加额外的开销并可能阻止使用索引。

关于mysql - 根据多列选择第二个表中不存在的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50065775/

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