如何得到BOM的尾阶的主件品名?表名BOMMD
MD001 --主件品名
select MD001,MD003 from BOMMD  where MD003='100001'
查询出结果是:
     
       600001     100001
       600002     100001
       600003     100001然后再查找
select MD001,MD003 from BOMMD  where MD003='600001'
得到
      810001     600001
      810002     600001select MD001,MD003 from BOMMD  where MD003='600002'
得到
      820001     600002
      820002     600002
      820003     600002select MD001,MD003 from BOMMD  where MD003='600003'
得到
      830001     600003
      830002     600003
select MD001,MD003 from BOMMD  where MD003='810001'
得到的是空值,
表明是尾阶材料了,在些以8开头的都是尾阶了
我想得到查询结果就是
      810001     600001
      810002     600001
      820001     600002
      820002     600002
      820003     600002
      830001     600003
      830002     600003

解决方案 »

  1.   

    http://blog.csdn.net/you_tube/archive/2009/04/28/4133435.aspx
      

  2.   

    --测试
    if object_id('BOMMD') is not null drop table BOMMD
    go
    create table BOMMD(MD001 varchar(10),MD003 varchar(10) )
    insert BOMMD
    select  600001, 100001 union all
    select  600002, 100001 union all
    select  600003, 100001 union all
    select  810001, 600001 union all
    select  810002, 600001 union all
    select  820001, 600002 union all
    select  820002, 600002 union all
    select  820003, 600002 union all
    select  830001, 600003 union all
    select  830002, 600003
    go
    select MD001,MD003 from BOMMD t
    where not exists(select 1 from BOMMD where MD003=t.MD001)--结果
    /*MD001      MD003      
    ---------- ---------- 
    810001     600001
    810002     600001
    820001     600002
    820002     600002
    820003     600002
    830001     600003
    830002     600003(所影响的行数为 7 行)*/