ID SPECID ORGNAME SUPERORGID CLASSID
.. ...... ....... .......... .......
10 0 专家组 9 ......
.. ...... ....... .......... .......
以上就是表结构,省略号表示数据,太多就不写出来了
我想实现的功能就是用1条SQL返回一个bool
判断条件有3个,用ID=10的数据来说(
1.SPECID=0;
2.ID=10上级ID(SUPERORGID存的就是上级ID,例如ID=10,上级ID=9)有很多,要去循环判断 每一个上级ID里的SPECID=0;
3.没有SUPERORGID=10的下级;
)
全部符合返回true,不符合返回false
.. ...... ....... .......... .......
10 0 专家组 9 ......
.. ...... ....... .......... .......
以上就是表结构,省略号表示数据,太多就不写出来了
我想实现的功能就是用1条SQL返回一个bool
判断条件有3个,用ID=10的数据来说(
1.SPECID=0;
2.ID=10上级ID(SUPERORGID存的就是上级ID,例如ID=10,上级ID=9)有很多,要去循环判断 每一个上级ID里的SPECID=0;
3.没有SUPERORGID=10的下级;
)
全部符合返回true,不符合返回false
就是没有该行ID的意思??
我用ID=10的这条记录举个例子,我要查出ID=10的上级和下级,并且判断每条记录中的SPECID==0
declare @t table(
ID int,
SPECID int,
SUPERORGID int)/*
--示例数据1
insert @t select 1, 0, 0
union all select 2, 0, 1
union all select 3, 0, 2
union all select 4, 0, 3
union all select 10,0, 1
union all select 10,0, 4--示例数据2
insert @t select 1, 0, 0
union all select 2, 0, 1
union all select 3, 0, 2
union all select 4, 1, 3
union all select 10,0, 1
union all select 10,0, 4--示例数据3
insert @t select 1, 0, 0
union all select 2, 0, 1
union all select 3, 0, 2
union all select 4, 0, 3
union all select 10,0, 1
union all select 10,0, 4
union all select 11,0, 10
*/declare @id int
set @id = 10select case when
(select count(1) from @t where id = @id) =
(select count(1) from @t t
where id = @id and
not exists(select 1 from @t
where id = t.superorgid and specid <> 0) and
not exists(select 1 from @t
where superorgid = t.id))
then 'true' else 'false' end
/*
--数据1结果
true--数据2结果
false--数据3结果
false
*/