你的描述有点错误,请你更正。
现在我想得到的结果: 
sid 姓名 时间      总课时  课程时间 剩余时间 
1,  tom,2008-08-01,20,    2,        18 
2,  tom,2008-08-08,20,    2,        16 
请问该如何关联,请高手指点 TOM 的编号怎么成了2?

解决方案 »

  1.   

    create table tb1(SID int,姓名 varchar(10) ,总课时 int)
    insert into tb1 values(1,'tom',  20 )
    insert into tb1 values(2,'jack', 30 )
    create table tb2(sid int,上课时间 datetime,课程时间 int)
    insert into tb2 values(1,'2008-08-01',2) 
    insert into tb2 values(1,'2008-08-08',2) 
    insert into tb2 values(2,'2008-08-01',3) 
    insert into tb2 values(2,'2008-08-08',3) 
    select a.SID, a.姓名,b.上课时间,a.总课时,b.课程时间,
      剩余时间 = (select 总课时 from tb1 where sid = a.sid) - (select sum(课程时间) from tb2 where sid = b.sid and 上课时间 <= b.上课时间)
     from tb1 a , tb2 b where a.sid = b.sid
    order by a.sid , b.上课时间drop table tb1,tb2/*
    SID         姓名         上课时间                                                   总课时         课程时间        剩余时间        
    ----------- ---------- ------------------------------------------------------ ----------- ----------- ----------- 
    1           tom        2008-08-01 00:00:00.000                                20          2           18
    1           tom        2008-08-08 00:00:00.000                                20          2           16
    2           jack       2008-08-01 00:00:00.000                                30          3           27
    2           jack       2008-08-08 00:00:00.000                                30          3           24(所影响的行数为 4 行)
    */