父子分類關(guān)系查詢使用的SQL語句介紹
作者:知識在于積累
SQL數(shù)據(jù)庫中,如果需要查詢父子分類關(guān)系,使用SQL語句應(yīng)該如何實現(xiàn)呢?下面就將為您介紹父子分類關(guān)系查詢使用的SQL語句的寫法,供您參考。
SQL數(shù)據(jù)庫中,如果需要查詢父子分類關(guān)系,使用SQL語句應(yīng)該如何實現(xiàn)呢?下面就將為您介紹父子分類關(guān)系查詢使用的SQL語句的寫法,供您參考。
例子如下圖:
查詢出來的結(jié)果多加一列,這一列的值為,當icode_ind有子分類,則該列的值為1,否則為0。是否有子類,看一下那表就很明顯我的規(guī)則了。
實現(xiàn)的SQL語句:
1.
select a.iCode_ind,a.icode,
case when b.iCode_ind is null then 0 else 1 end
from TabA a outer apply (select top 1 iCode_ind from TabA
where icode_ind like a.icode_ind+'%' and icode_ind<>a.icode_ind) b
2.
select *,case when exists(select 1 from tb
where iCode_ind<>t.iCode_ind
and iCode_ind like t.iCode_ind+'%')
then 1 else 0 end
from tb t
3.
select iCode_ind,icode,col=case when exists(select 1 from 表名 where iCode_ind like a.iCode_ind+'%' and iCode_ind!=a.iCode_ind) then 1 else 0 end
from 表名 a
【編輯推薦】
責任編輯:段燃
來源:
博客園