create  table  tt(ch  varchar(20)) 
 
insert  tt  select  '1001001' 
union  all  select  '1001' 
union  all  select  '2001001' 
union  all  select  '2001' 
union  all  select  '300120013001' 
union  all  select  '4001' 
 
--求森林的根 
select  *   
from  tt  as  t1 
where  not  exists 
           (select  1 
           from  tt  as  t2 
           where  t1.ch  like  t2.ch+'%' 
           and  t1.ch!=t2.ch)   
--求森林的叶 
select  *   
from  tt  as  t1 
where  not  exists 
           (select  1 
           from  tt  as  t2 
           where  t2.ch  like  t1.ch+'%' 
           and  t1.ch!=t2.ch)   

相关文章:

  • 2022-01-16
  • 2021-08-24
  • 2022-12-23
  • 2022-01-02
  • 2021-08-10
猜你喜欢
  • 2021-07-08
  • 2021-09-28
  • 2021-12-09
  • 2022-12-23
相关资源
相似解决方案