有这样一个表,wy_yslb,字段有yslb_id,yslb_cname,yslb_parentid,yslb_class,yslb_mjflg.一个树形结构,yslb_mjflg = '1'即为最末级。
例如: yslb_id yslb_cname yslb_parentid yslb_class yslb_mjflg
1 A 0 1 0
2 BBB 0 1 0
3 a 1 2 0
4 aaa 3 3 1
5 b 2 2 1
现在给yslb_mjflg = '1' 的yslb,能用一个sql语句得到他的上级包括他本身吗?望指教下呵
SQL code:
--> 测试数据: #T1
if object_id('tempdb.dbo.#T1') is not null drop table #T1
create table #T1 (yslb_id int,yslb_cname varchar(3),yslb_parentid int,yslb_class int,yslb_mjflg int)
insert into #T1
select 1,'A',0,1,0 union all
select 2,'BBB',0,1,0 union all
select 3,'a',1,2,0 union all
select 4,'aaa',3,3,1 union all
select 5,'b',2,2,1
select * from #T1 where yslb_mjflg = '1'
union all
select * from #t1 where yslb_id in (select yslb_parentid from #T1 where yslb_mjflg = '1' )
yslb_id yslb_cname yslb_parentid yslb_class yslb_mjflg
----------- ---------- ------------- ----------- -----------
4 aaa 3 3 1
5
现在有两张表:文章主表A(articleId,articleTitle),文章评论表B(commentId,articleId,commentTitle)
现在我想实现这样的功能:列出文章列表,其中每篇文章标题下面列出此文章的前2个文章评论,请问sql语句怎么写啊 ......