●●●●●有点容易但又不容易想起来的问题●●●●●

zcwmxn 2005-03-16 02:07:29
树状图结构表(t_function)
id int
parentid int
name int

用一个sql语句(不是存储过程一起其他,仅仅用select)把所以叶子节点查询出来
请指教。
...全文
287 33 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhzhao2002 2005-03-31
  • 打赏
  • 举报
回复
千里猪:你的思想是对的。但是你的“SELECT *
FROM t_function
where not exists(select * from t_function a where a.parent = id)

写错了。select * from t_function a where a.parent = id这个结果将是个空集。所以总的结果将是整个表。
jhzhao2002 2005-03-31
  • 打赏
  • 举报
回复
你只需要查 id 在 parentid 中不存在。
select id,name from table where id not in (select parentid from table)

就行了。
rocklabzhang 2005-03-30
  • 打赏
  • 举报
回复
mark
hww2004 2005-03-30
  • 打赏
  • 举报
回复
mark
wjlsmail 2005-03-30
  • 打赏
  • 举报
回复
Mark
zcwmxn 2005-03-29
  • 打赏
  • 举报
回复
你去用那个什么english query的东东,或者那里面有

这位仁兄,能否介绍一下看哪部分的资料?
zheninchangjiang 2005-03-24
  • 打赏
  • 举报
回复
真感觉无聊
zheninchangjiang 2005-03-24
  • 打赏
  • 举报
回复
你去用那个什么english query的东东,或者那里面有
cocopww 2005-03-21
  • 打赏
  • 举报
回复
晚了点,不好意思,请大家给点意见
cocopww 2005-03-21
  • 打赏
  • 举报
回复
create table A1
(qianID char(20) null,
houID char(20) null,
menber int null)

---------------------------

select *
into #@linshibiao
from A1
where qianID = 'a'

insert jieguo
select * from A1
where qianID = 'a'

while exists(select a.qianID,b.houID,a.menber*b.menber as menber from #@linshibiao a join A1 b on (a.houID = b.qianID and a.qianID = 'A'))
begin
select a.qianID,b.houID,a.menber*b.menber as menber
into #@zhongjianbiao
from #@linshibiao a join A1 b
on (a.houID = b.qianID and a.qianID = 'A')

insert jieguo
select a.qianID,b.houID,a.menber*b.menber as menber
from #@linshibiao a join A1 b
on (a.houID = b.qianID and a.qianID = 'A')

truncate table #@linshibiao
insert into #@linshibiao
select * from #@zhongjianbiao
drop table #@zhongjianbiao
lishengyu 2005-03-19
  • 打赏
  • 举报
回复
如果不是固定的可参考下面的语句

create table tb(id int identity(1,1) not null constraint PK_tb primary key clustered
,parentID int,name varchar(20))
insert into tb
select 0,'中国'
union all select 0,'美国'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江苏'
union all select 6,'苏州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'无锡'
union all select 2,'纽约'
union all select 2,'旧金山'
go

declare @i int
set @i=1
create table #tmp(id int,px varchar(1000),parentid int)
insert into #tmp select [id], '', parentID from tb order by id

update #tmp set px=cast(@i+10000000 as varchar) ,@i=@i+1
from #tmp where parentid=0

while @@rowcount>0
update b set b.px=a.px+cast(@i+10000000 as varchar),@i=@i+1
from #tmp a join #tmp b
on a.id=b.parentID and a.px<>'' and b.px=''

select b.*,space(len(a.px)/2)+b.name 缩进的 from
#tmp a join tb b on a.id=b.id order by a.px
drop table #tmp
drop table tb


id parentID name 缩进的
----------- ----------- -------------------- --------------------------
1 0 中国 中国
4 1 北京 北京
5 1 上海 上海
6 1 江苏 江苏
7 6 苏州 苏州
8 7 常熟 常熟
9 6 南京 南京
10 6 无锡 无锡
2 0 美国 美国
11 2 纽约 纽约
12 2 旧金山 旧金山
3 0 加拿大 加拿大

(所影响的行数为 12 行)
lishengyu 2005-03-19
  • 打赏
  • 举报
回复
如果这颗树的深度是固定的,只用一句SQL语句就可以实现
haslong 2005-03-19
  • 打赏
  • 举报
回复
ding
real_name 2005-03-19
  • 打赏
  • 举报
回复
是不是少了一个 字段 啊
wangdehao 2005-03-19
  • 打赏
  • 举报
回复
select id,name from t_function where id not in(select parentid from t_function )
zcwmxn 2005-03-18
  • 打赏
  • 举报
回复
没错,其实 jinjazz(近身剪(N-P攻略)) 的方法已经比较叫精炼了。
zjcxc 元老 2005-03-17
  • 打赏
  • 举报
回复
"两个临时表 再加一个while"

那和用存储过程有什么区别呢? 把一楼的函数截头截尾,还只用了一个表变量+一个while循环
zcwmxn 2005-03-17
  • 打赏
  • 举报
回复
既然 qyj2000188() 需要, cocopww(吧啦) 兄弟就把你的临时表贴出来,让他们学习学习

不过在此仍然求只用select 复杂语句能查询到结果的sql语句----不用存储过程,不用临时表、不用函数的方法。
qyj2000188 2005-03-16
  • 打赏
  • 举报
回复
很有必要呈上来,看看!
ohui 2005-03-16
  • 打赏
  • 举报
回复
学习
加载更多回复(13)

34,837

社区成员

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

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