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

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

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

例子如下图:

  

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

实现的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

 

 

 【编辑推荐】

SQL语句中output的用法

详解SQL中的GROUP BY语句

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

SQL Server日期计算语句

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

 

责任编辑:段燃 来源: 博客园
相关推荐

2010-09-07 10:35:38

SQL语句

2010-10-27 15:11:52

oracle递归查询

2023-11-14 10:03:30

数据库技术

2010-11-12 14:10:15

SQL遍历父子关系表

2010-07-08 13:26:02

SQL Server

2010-10-21 10:28:13

SQL Server查

2010-09-25 16:32:02

SQL语句

2023-03-31 14:15:57

SQLORDER BY

2010-10-21 12:16:11

SQL Server查

2010-10-27 10:11:07

Oracle分页查询

2010-09-26 15:23:24

SQL语句

2010-09-25 15:26:45

SQL查询语句

2010-03-15 19:11:39

Java多线程语句

2010-09-08 17:10:24

SQL循环语句

2010-11-11 11:37:22

SQL SELECT语

2010-09-28 14:33:13

SQL语句

2017-09-07 16:20:39

SQL查询语句查询优化

2019-11-06 09:30:35

SQL查询语句数据库

2010-11-12 13:40:38

动态sql语句

2010-10-22 15:52:41

SQL Server创
点赞
收藏

51CTO技术栈公众号