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

解决方案 »

  1.   

    测试:
    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 行)
      

  2.   

    大虾zjcxc(邹建) :
    表: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 !!!
      

  3.   

    declare @jc int
    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