什么不存在查询不工作

时间:2013-05-30 16:57:39

标签: sql-server tsql

我正在尝试查明inventory_detail表中是否有任何订单(订单号实际上在inventory_header上),其中loadinfo或者loadload_store值根据extrainfo_stores表中的store_Code字段中根本不存在。< / p>

下面的查询返回一些订单,当我在extrainfo_stores表中查看返回的订单时,我看到该表中的load_store或unload_store中的确切值,所以它显然不起作用...

 SELECT  bh.ord_number, bi.*
 FROM    inventory_header bh
 inner join inventory_detail bi on inventory_id = bh.inventory_id
 WHERE   NOT EXISTS
    (
    SELECT  null 
    FROM    extrainfo_stores d
    WHERE   d.ord_number = bh.ord_number
      and   bi.load_store <> d.store_code
      and   bi.unload_store <> d.store_code
    )

2 个答案:

答案 0 :(得分:1)

应该是这样的:

SELECT bh.ord_number, bi.*
  FROM inventory_header bh
 inner join inventory_detail bi on inventory_id = bh.inventory_id
 WHERE NOT EXISTS (SELECT null 
                     FROM extrainfo_stores d
                    WHERE d.ord_number = bh.ord_number
                      and (bi.load_store = d.store_code or bi.unload_store = d.store_code))

答案 1 :(得分:0)

这是你在找什么?

SELECT bh.ord_number, bi.*
 FROM  inventory_header bh
 join  inventory_detail bi 
   on  inventory_id = bh.inventory_id
 join  extrainfo_stores d
   on  d.ord_number = bh.ord_number
Where bi.load_store   <> d.store_code
   or bi.unload_store <> d.store_code