如下表
母件  组件
3300  2400
3300  1100
3300  2200
3210  2300
3210  2400
3210  1200
2400  2200
2400  1004
2200  1003
2300  2210
2300  1120
2210   1000
结果要求得到:
母件  非叶子
3300  3300
3300  2200
3300  2400
3300  2200
3210  3210
3210  2400
3210  2200
3210  2300
3210  2210
2200  2200
2300  2300
2300   2210
2400   2400
2400   2200
也就是母件作为一个数的根结点,现在要求要得到所有母结点的非叶子结点

解决方案 »

  1.   

    select * from (
    select * from tb a 
    where not exists(select 1 from tb where 母件=a.组件)
    union all 
    select distinct 母件,母件 from tb) a order by 母件 desc
      

  2.   

    1楼好像错了select * from (
    select 母件,组件 as 非叶子
     from tb a 
    where exists(select 1 from tb where 母件=a.组件)
    union all 
    select distinct 母件,母件 from tb) a order by 母件 desc楼主好像也错了,结果应该增加一条
    2210    2210
      

  3.   

    not exists是叶子
    exists是非叶子
      

  4.   

    select * from (
    select * from tb a 
    where not exists(select 1 from tb where 母件=a.组件)
    union all 
    select distinct 母件,母件 from tb) a order by 母件 desc