zoukankan      html  css  js  c++  java
  • Oracle:Not exists

    I agree with using not exists.  
    
    Consider a case where you want to perform more logic than just "One not in the other".
    
    Consider Item, Sale, and SaleDetail:
    
    create table Item
    (
      ItemId number(6),
      ItemDescription varchar(200),
      UnitPrice number(18,6)
    )
    /
    Create table sale
    (
      SaleId number(6),
      TransactionDate date
    )
    /
    create table SaleDetail
    (
      SaleId number(6),
      ItemId number(6),
      Quantity number(18)
      
    )
    
    
    insert into item
    values (1,'Toyota Prius',18500.95)
    /
    insert into item
    values (2,'Lock Washer', .004)
    /
    
    insert into sale
    values (1000,'15-Jun-2008')
    /
    insert into sale
    values (1001,'23-Aug-2008')
    /
    insert into sale
    values (1002,'05-Jan-2009')
    /
    insert into sale
    values (1003,'09-Jan-2009')
    /
    insert into sale
    values (1004,'15-Feb-2009')
    /
    
    insert into SaleDetail
    values (1000, 1, 1)
    /
    insert into SaleDetail
    values (1001, 1, 2)
    /
    insert into SaleDetail
    values (1002, 1, 2)
    /
    
    insert into SaleDetail
    values (1003, 2, 1000)
    /
    insert into SaleDetail
    values (1004, 2, 1900)
    /
    
    Now, I want to know which items from the item table have yet to be sold in 2009 in a quantity 
    greater than 100 (what have i not yet sold in bulk this year?).
    
    
    Two alternatives using outer join logic (both nasty to read and comprehend):
    
    select i.*
    from item i
    left outer join saleDetail sd
    ON i.itemId = sd.ItemId
    AND sd.quantity > 100
    Left outer join sale s
    ON sd.saleId = s.saleid
    and  trunc(s.transactionDate,'YYYY') = to_date('01-JAN-2009','DD-MON-YYYY')
    WHERE sd.itemId is null     
    
    or
    
    
    select i.*
    from item i
    left outer join 
        (select sd_i.* 
            from saleDetail sd_i
            JOIN sale s
            on s.saleid = sd_i.saleid
            where trunc(s.transactionDate,'YYYY') = to_date('01-JAN-2009','DD-MON-YYYY')) sd
            
        ON i.itemId = sd.ItemId
    AND sd.quantity > 100
    WHERE sd.itemId is null      
    
    
    Now again, using the not exists (much  easier to read and much easier to test what HAS been sold in 
    bulk in 2009 by removing the inner to outer join on itemId):
    
    select *
    from item i
    where not exists (select 'x' 
                        from saleDetail sd
                        JOIN sale s
                        ON sd.saleId = s.saleId
                        where sd.itemId =i.itemId
                        and quantity > 100
                        and trunc(s.transactionDate,'YYYY') =
                          to_date('01-JAN-2009','DD-MON-YYYY')
                        )
    
    
    Many developers would even try moving the where-like and conditions in the outer join syntax into 
    the body of the where clause, thinking that it was just misplaced initially.  Once it's in the 
    where, you've created an inner join dataset and no inner join would have the "non-inner-join anti 
    join" condition.
    
  • 相关阅读:
    CSS文字的处理
    typeof 检测变量的数据类型
    BZOJ 1257: [CQOI2007]余数之和
    BZOJ 1218: [HNOI2003]激光炸弹
    BZOJ 3251: 树上三角形
    BZOJ 3916: [Baltic2014]friends
    BZOJ 1610: [Usaco2008 Feb]Line连线游戏 暴力
    BZOJ 1593 [Usaco2008 Feb]Hotel 旅馆 双倍经验,线段树
    BZOJ 1096 [ZJOI2007]仓库建设 BZOJ 3437 小P的牧场 BZOJ 3156 防御准备 斜率优化dp
    BZOJ 2582 : Bovine Alliance DFS
  • 原文地址:https://www.cnblogs.com/tracy/p/2125292.html
Copyright © 2011-2022 走看看