【问题标题】:(SQL) How to check if a value is in another table?(SQL) 如何检查一个值是否在另一个表中?
【发布时间】:2020-04-19 13:26:58
【问题描述】:

我不太擅长 SQL,所以我很抱歉。

我希望能够遍历表 A 上的每一行并检查表 B 的整个列中是否存在特定值。

我想查看表 A 中值不在表 B 特定列中的所有行。

我希望这是有道理的。

【问题讨论】:

    标签: sql join select


    【解决方案1】:

    您可以使用not exists。你的问题有点理论,但逻辑是:

    select a.*
    from tablea a
    where not exists (select 1 from tableb b where b.col1 = a.col1)
    

    tabla(col1) 中的值应与tableb(col1) 中的值相对应。

    【讨论】:

    • 欢迎@JoeBrown。如果我的回答正确回答了您的问题,请点击复选标志accept it。谢谢。
    【解决方案2】:

    听起来像not exists

    select a.*
    from a
    where not exists (select 1 from b where b.col = a.col);
    

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2021-01-17
      • 2021-10-27
      相关资源
      最近更新 更多