【问题标题】:select query with Not IN keyword选择带有 Not IN 关键字的查询
【发布时间】:2013-08-24 09:11:00
【问题描述】:

我有两张表如下..

  1. tbPatientEncounter
  2. tbVoucher

当我如下执行select query

Select EncounterIDP,EncounterNumber from tbPatientEncounter

它返回了 180 行。和

Select VoucherIDP,EncounterIDF from tbVoucher

上面的查询返回 165 行。

但我想执行选择查询,该查询返回我的数据,如 EncounterIDP 不在 tbVoucher 中,因为我在选择查询下面尝试过...

    Select * from tbPatientEncounter pe 
    where pe.EncounterIDP not in 
    (Select v.EncounterIDF from tbVoucher v )

它不返回任何行。在第一张图片中它显示了EncounterIDP 9 in tbPatientEncounter,但它没有插入到 tbVoucher 中,因为我尝试过 select Query like

Select * from tbVoucher where EncounterIDF = 9 

它返回 0 行。

我的问题是what is wrong with my above Not In Query.?

【问题讨论】:

    标签: sql sql-server select notin


    【解决方案1】:

    问题很可能是tbVoucher 中的NULL 值。试试这个:

    Select *
    from tbPatientEncounter pe 
    where pe.EncounterIDP not in (Select v.EncounterIDF
                                  from tbVoucher v
                                  where v.EncounterIDF is not NULL
                                 )
    

    【讨论】:

    • @Gordon Linoff 谢谢你的回答,但你怎么知道EncounterIDF colomn 有空值?
    【解决方案2】:

    您是否在比较 tbVoucher 中的正确字段?

    尝试使用左连接

    Select EncounterIDP,EncounterNumber from tbPatientEncounter
           left join tbVoucher on EncounterIDP = EncounterIDF
    where EncounterIDF is null
    

    【讨论】:

    • @podiluska 我想用not in 查询。我的查询出了什么问题。
    【解决方案3】:

    请称我为怀疑论者,因为我认为您的查询没有任何问题。这真的是查询中的全部内容,还是您为我们简化了?

    Select * from tbPatientEncounter pe 
    where pe.EncounterIDP not in 
    (Select v.EncounterIDF from tbVoucher v )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      相关资源
      最近更新 更多