【问题标题】:Joing two tables showing me records from one tables where it doesn't have a corresponding value in another table连接两个表,显示一个表中的记录,而另一个表中没有对应的值
【发布时间】:2017-06-11 03:16:39
【问题描述】:

所以我有两个表,我正在尝试进行连接,以显示一个表中的记录在另一个表中没有对应的值....

tbl1

ClientNo      DateRec       CompID
123           1/2/2017      5558
124           1/3/2017      5558
234           1/3/2017      5558


tbl2
ClientNO      CompID
123           5558
124           5558

所以这里的一切都基于 CompID = 5558。这是一个神奇的数字。我希望加入这两个表,并且只显示来自 tbl1 的记录 - 其中 tbl2 没有相同 clientID 和 5558 的记录。

期望的最终结果:

 ClientNo      DateRec       CompID
 234           1/3/2017      5558

这是我想要的最终结果,因为客户端 234 在 tbl2 中没有相同的 CompID。所以我只向我显示来自 tbl1 的记录,其中 tbl2 没有相同的 CompID(在本例中为 5558)

【问题讨论】:

    标签: sql join sql-server-2012


    【解决方案1】:

    你已经描述了很多not exists

    select t.*
    from tbl1 t
    where not exists (select 1 from tbl2 t2 where t2.ClientNo = t.ClientNo and t2.CompId = t.CompId);
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 1970-01-01
      • 2013-04-03
      • 2010-12-31
      • 1970-01-01
      • 2020-01-15
      • 2014-09-25
      • 2021-03-09
      • 2021-12-10
      相关资源
      最近更新 更多