在一个 table 中找到一个存在于另一个 table 的多个列中的值

Find a value in one table that exists in multiple columns of another table

我们有一个 table 的 CUSTOMER 和一个 table 的 ACCOUNTS(table 中都没有主键或外键 - 长话短说!)

我的数据是这样的:

帐户可以有 1、2 或 3 个所有者。 我需要找到哪些客户未与任何帐户相关联。

我尝试过的:

但是查询确实需要很长时间 - 即使我将子查询限制为 return 前 10 行。

我想在我的搜索中看到 returned Danny、Emma 和 Fang

您忘记将子查询关联到您的主查询。您的查询说:“如果帐户 table 中没有行,请给我所有客户。”它应该说“给我所有客户 在帐户 table 中没有行。”

select cutomername
from customer c
where not exists
(
  select null
  from accounts a
  where c.customername in (a.owner1, a.owner2, a.owner3)
);