本帖最后由 kavaye 于 2012-09-20 19:18:26 编辑

解决方案 »

  1.   


    查询的是:表OrderItem中顾客商品号没有任意一条是和顾客0002中商品号相对应的所有顾客记录也就是说顾客0001如果有一条商品记录在0002的商品记录中存在,则就不会被选出来
      

  2.   

    子查询的分析方式一般是从内向外:select 商品号 from OrderItem Z where Z.顾客号=X.顾客号查询当前顾客购买的全部商品号select 商品号 from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号)查询'0002'号顾客购买了,而当前顾客没有顾客没有购买的全部商品号select distinct 顾客号 from OrderItem X where not exists 
    (select * from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号))查询不存在('0002'号购买了但自己没有购买)这种情况的顾客号。
    即:查询“'0002'号购买的商品自己都已购买”的顾客编号。
      

  3.   

    如果LZ学过离散的话可以看下:
    查询顾客购买的商品集记为A,'0002'号顾客购买的商品集记为B。select 商品号 from OrderItem Z where Z.顾客号=X.顾客号查询的结果为A;select 商品号 from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号)查询结果为A∩¬B(¬A为A的补集,PS:在下懒得去找补集符号了,随便找了个像点的代替);select distinct 顾客号 from OrderItem X where not exists 
    (select * from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号))查询条件是A∩¬B为空集,计算:
    A∩¬B=Ø  =>  A⊇B
    B为A的子集,即查询条件为:'0002'号顾客购买的商品都买了。
      

  4.   


    select distinct 顾客号 --找出不同的顾客号
    from OrderItem X  --从 OrderItem 表,别名X中
    where not exists -- 筛选X的条件是不存在以下情况 
    (select * from OrderItem Y where 顾客号='0002' --商品在002那
    and not exists (select * from OrderItem Z 
    where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号))--且不再自己这
    -- 即 筛选出所有  不存在(某商品002有而“我”没有) 中的“我”
    --换一说法:如果 (某商品002有而“我”没有)这一情况不存在,那我就是符合条件的顾客
      

  5.   

    --整句查询
    select distinct 顾客号 from OrderItem X where not exists 
    (select * from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号))
    --先考虑下以下查询
    select * from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号))
    --select * from OrderItem Y where 顾客号='0002'  为0002顾客的全部商品
    --加上and not exists (select * from OrderItem Z where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号))
    --意思为0002顾客的所有商品中不包含X顾客商品的记录,如果这样的记录存在,
    --证明0002顾客的商品中有x顾客商品中未有的
    --再考虑
    select distinct 顾客号 from OrderItem X
    --对x表的每一条记录,都执行上面的查询,假设第一条记录的顾客号为'0003'
    --执行
    select * from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号='0003' and Z.商品号=Y.商品号))
    --如果存在记录,说明顾客'0002'的商品中含有顾客'0003'未订购的商品
    --反过来,只有以上查询不存在记录,才能说明'0003'的商品已包含顾客'0002'的全部商品
    --所以加上not exists 变为
    not exists(
    select * from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号='0003' and Z.商品号=Y.商品号)))
    --再加上select * from orderitem X 就是全部整条查询的内容
      

  6.   

    为了方便理解,建了个 OrderItem数据表
    OrderItem
    顾客号    商品号                 
    0001      01
    0001      02
    0001      03
    0002      01
    0002      02
    0003      01
    0003      04
    0004      04
    0004      05 里面的 exists 语句可以写成
    select * from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号=@顾客号 and Z.商品号=Y.商品号)其中@顾客号代表一个变量,表示 '0001','0002','0003','0004'
    当@顾客号 = '0001'时,结果是 空集  '0002'时,结果也是空集;
    当@顾客号 = '0003'时,结果是 '0002','02'  '0004'时,结果是 '0002','01';'0002','02';
    从上面可以看出,这个语句的作用其实就是 显示每个顾客号没有买0002所订购的商品;
    select distinct 顾客号 from OrderItem X where not exists 
    (select * from OrderItem Y where 顾客号='0002' and not exists
    (select * from OrderItem Z where Z.顾客号=X.顾客号 and Z.商品号=Y.商品号))因为前面 '0001','0002'的结果集是空集,'0003','0004'存在有没买的0002的订购的商品,其结果集不为空集,在外面的NOT EXISTS的作用下,只有顾客号 '0001','0002'显示记录,再与OrderItem X用 
    Z.顾客号=X.顾客号 关联,其结果就显示 '0001','0002'的顾客号,即买了0002所订购的所有商品的 顾客号结果集