求一SQL怎么写,查找是否有子节点

anbin0814 2011-08-11 10:16:24
code name order degree needsub infoget
01 事业部 0 一级 1 NULL
0101 上海分公司 0 二级 0 NULL
0102 浙江分公司 0 二级 0 NULL
0104 江西分公司 0 二级 0 NULL
0105 江苏分公司 0 二级 0 NULL
010204 系统管理部 0 三级 1 0
010201 公司领导 0 三级 1 0
010202 行政综合部 0 三级 1 0
010203 财务部 0 三级 1 0
010205 物流部 0 三级 1 0
010206 生产部(生产组) 0 三级 1 0
010207 生产部 0 三级 1 0
010208 安全环保部 0 三级 1 0
010209 合成车间 0 三级 1 0
010210 精馏一车间 0 三级 1 0
010211 EF车间 0 三级 1 0
010212 设备部 0 三级 1 0
010213 品管部 0 三级 1 0
010214 技术部 0 三级 1 0
010215 硝化车间 0 三级 1 0
010216 制氢车间 0 三级 1 0
010217 精馏一车间(DCBA) 0 三级 1 0
010218 精馏三车间 0 三级 1 0
010219 H101车间 0 三级 1 0
0106 上海销售分公司 0 二级 0 NULL
010101 发展部 0 三级 0 NULL
010103 国际贸易部 0 三级 0 NULL
010104 财务部 0 三级 0 NULL
010106 物流部 0 三级 0 NULL
0103 北京代理处 0 二级 0 NULL
010102 内贸部 0 三级 0 NULL

根据code列判断是否有子节点
是否有子节点判断的方法是 0101 如果有子节点就有010101,
...全文
466 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
anbin0814 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ssp2009 的回复:]

引用 10 楼 anbin0814 的回复:
AcHerat 已经解决,爱死你了,呵呵,结贴
嚓,我只是少写个when啊
[/Quote]

呵呵,
geniuswjt 2011-08-11
  • 打赏
  • 举报
回复
select * from tb a where not exists(select 1 from tb where a.code=left(code,len(a.code)) and and code<>a.code)
and code<>a.code)
[Quote=引用 8 楼 anbin0814 的回复:]
引用 7 楼 geniuswjt 的回复:

你想得到的结果是什么样的?!


想得到没有子节点的节点
[/Quote]
快溜 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 anbin0814 的回复:]
AcHerat 已经解决,爱死你了,呵呵,结贴
[/Quote]嚓,我只是少写个when啊
anbin0814 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 acherat 的回复:]

SQL code

select *
from tb a
where not exists (select 1 from tb where a.code=left(code,len(a.code)) and len(code)>len(a.code))
[/Quote]

这种效率好?
AcHerat 元老 2011-08-11
  • 打赏
  • 举报
回复

select *
from tb a
where not exists (select 1 from tb where a.code=left(code,len(a.code)) and len(code)>len(a.code))
anbin0814 2011-08-11
  • 打赏
  • 举报
回复
AcHerat 已经解决,爱死你了,呵呵,结贴
快溜 2011-08-11
  • 打赏
  • 举报
回复
select * from tb
where not exists (select 1 from tb where a.code=left(code,len(a.code))
and code<>a.code)
anbin0814 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 geniuswjt 的回复:]

你想得到的结果是什么样的?!
[/Quote]

想得到没有子节点的节点
geniuswjt 2011-08-11
  • 打赏
  • 举报
回复
你想得到的结果是什么样的?!
AcHerat 元老 2011-08-11
  • 打赏
  • 举报
回复

select case when exists(select 1 from tb where a.code=left(code,len(a.code))
and code<>a.code) then '存在' else '不存在' end
from tb a
anbin0814 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ssp2009 的回复:]

SQL code
select case exists(select 1 from tb where a.code=left(code,len(a.code))
and code<>a.code) then '存在' else '不存在' end
from tb a
[/Quote]


SELECT CASE exists(SELECT 1 FROM Department WHERE D.deptcode=LEFT(deptcode,len(D.deptcode))
AND deptcode<>D.deptcode) THEN '存在' ELSE '不存在' end
FROM Department AS D

消息 156,级别 15,状态 1,第 1 行
关键字 'exists' 附近有语法错误。
消息 156,级别 15,状态 1,第 2 行
关键字 'THEN' 附近有语法错误。
快溜 2011-08-11
  • 打赏
  • 举报
回复
select case exists(select 1 from tb where a.code=left(code,len(a.code)) 
and code<>a.code) then '存在' else '不存在' end
from tb a
快溜 2011-08-11
  • 打赏
  • 举报
回复
select case exists(select 1 from tb where a.code=left(code,len(code)) 
and code<>a.code) then '存在' else '不存在' end
from tb a
天-笑 2011-08-11
  • 打赏
  • 举报
回复
select case when count(1)>1 then '有子节点' else '无' end from tab where code like '0101%' and code <>'0101'
geniuswjt 2011-08-11
  • 打赏
  • 举报
回复
select * from tb where substring(code,5,2)<>''

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧