select
cno,
(select cno from course where cno=a.pcno),
(select cno from course where cno=(select pcno from course where cno=a.pcno))
from course a where cno=2
cno,
(select cno from course where cno=a.pcno),
(select cno from course where cno=(select pcno from course where cno=a.pcno))
from course a where cno=2
解决方案 »
- 客户端A能访问应用服务器B,应用服务器B能访问数据库C
- sqlserver2005安装不上
- 用as别名条件查询问题,出现列名无效
- 如何根据数量按时间删除数据
- 请问如何用log文件恢复数据库?我还有一个2002/12/7的备份,现在mdf文件不能用sp_attach_single_file_db 了!
- 高分告急:SQL Server 2005 怎样建库可以让 备份或者其它方式 在 SQL Server 2000 内恢复并使用
- T-sql 与sql 差异
- SQL*PLUS脚本运行问题。
- 如何提高远程访问SQL SERVER 的速度?
- server 2008 R2 安装后 无法登陆 login failed for user
- 如何对查询结果进行分组加序号
- 邹建,马可..各位,再帮帮忙!!!
create table t1(cno int,cn varchar(10),pcno int)
insert t1 values(1,'yuwen',2)
insert t1 values(2,'shuxue',3)
insert t1 values(3,'wuli',4)
insert t1 values(4,'shengwu',5)select a.cno,b.cn,c.cn
from t1 a
left join t1 b on a.pcno = b.cno
left join t1 c on b.pcno = c.cnocno cn cn
----------- ---------- ----------
1 shuxue wuli
2 wuli shengwu
3 shengwu NULL
4 NULL NULL(所影响的行数为 4 行)
表:course(cno,cn,pcno)
cno:课程号
cn:课程名
pcno:先行课程号
如:
cno cn pcno
1 语文 2
2 数学 3
3 物理 4
4 生物 5cno为1的pcno为2,cno为2的pcno为3,cno为3的pcno为4....
则 1的直接先行为2,间接先行为3,4
即:
cno 直接 间接
1 2 3
1 2 4
1 2 5
2 3 4
2 3 5
......thank you !!!
select @jc=1 --循环次数 select a.con acon,b.con bcon,b.pcon,@jc jc into #tmp from course a,course b where a.pcon=b.conwhile (select count(*) from #tmp c,course d where c.pcon=d.con and c.jc=@jc)<>0
begin
insert into #tmp select a.acon,a.bcon,b.pcon,@jc+1 from #tmp a,course b where a.pcon=b.con and a.jc=@jc
select @jc=@jc+1
endselect * from #tmp order by acon,bconacon bcon pcon jc
1 2 4 2
1 2 5 3
1 2 3 1
2 3 5 2
2 3 4 1
3 4 5 1