父子分类关系查询使用的SQL语句介绍

SQL数据库中,如果需要查询父子分类关系,使用SQL语句应该如何实现呢?下面就将为您介绍父子分类关系查询使用的SQL语句的写法,供您参考。

例子如下图:

  

查询出来的结果多加一列,这一列的值为,当icode_ind有子分类,则该列的值为1,否则为0。是否有子类,看一下那表就很明显我的规则了。

实现的SQL语句:

1.

select a.iCode_ind,a.icode,
casewhen b.iCode_ind isnullthen0else1end
from TabA a outer apply (selecttop1 iCode_ind from TabA
where icode_ind like a.icode_ind+'%'and icode_ind<>a.icode_ind) b

2.

select*,casewhenexists(select1from tb 
where iCode_ind<>t.iCode_ind
and iCode_ind like t.iCode_ind+'%')
then1else0end
from tb t

3.

select iCode_ind,icode,col=casewhenexists(select1from 表名 where iCode_ind like a.iCode_ind+'%'and iCode_ind!=a.iCode_ind) then1else0end
from 表名 a

 

 

 【编辑推荐】

SQL语句中output的用法

详解SQL中的GROUP BY语句

sqlplus执行存储过程和sql语句的写法

SQL Server日期计算语句

为您介绍一些不常见的SQL语句

 

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/236950.html<

(0)
管理的头像管理
上一篇2025-04-22 06:16
下一篇 2025-04-22 06:18

相关推荐

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注