易截截图软件、单文件、免安装、纯绿色、仅160KB

急求一个sql语句!! - MS-SQL Server / 基础类

有这样一个表,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


相关问答:

Asp+sql server问题 - Web 开发 / ASP

我一个项目,有个插入操作,具体是这样的:
我有进货信息表。在出货时选择相应的进货信息,输入数量,选择部门后,点保存按钮,由于网络延时,点一下没有反映,于是用户就又点一下,导致一次插入了两条记录:
例: ......

SQL如何优化问题 - MS-SQL Server / 疑难问题

今天做了一个存储过程   环境是SQL2000数据库  
大致如下
建立临时表
定义员工游标
        循环员工(属于1个公司)  
        ......

请教SQL语句查询问题? - MS-SQL Server / 基础类

我想查询出每天数据的最大的一个值。表的格式如下
表名: hisdata
字段 编号 值 状态 时间  
  Id value state dattime  
  101 32.3 0 ......

mysql区没高手 - MS-SQL Server / 基础类

我觉得mysql和sqlserver有共同的地方:
有个问题是关于表的锁问题:
进程A 进程B
select * from user where id in lock share mode(共享锁)
&nb ......

sql语句问题 - MS-SQL Server / 疑难问题

现在有一个部门表dept(部门名称,部门号。。)有一个人员表emp(姓名,人员编号,职位,薪资,部门)
emp表中的内容是这样的:
a 1 工程师 3000 软件部
b 2 普通员工 2000 硬件部
c 3 工程师 4000 硬件部
d ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号