学生表 [student] id name isDelete学生成绩表[record] id time result studentId学生和学生成绩表是1-N关系,就是一个学生会有多次成绩
我现在要查出每个学生“最近一次成绩”>100的学生,一行sql应该怎么写好像不难,但是我太弱了id都是seq自增长的

解决方案 »

  1.   

    结构我有写啊
    T_Student表就是id name isDelete三个字段 
    T_Record表就是id time result studentId四个字段其中Record根据studentId和Student的id关联
      

  2.   

    貌似这2个表没有可以关联的字段阿
    select result,studentid from record a where result >100 and time =(select max(time) from record b where a.studentid=b.studentid group by b.studentid,b.result having b.result >100 )试试看勒
      

  3.   


    select   name,studentid,result   from   record   a   where  id=studentid and result   > 100   and   time   =(select   max(time)   from   record   b   where   a.studentid=b.studentid   group   by   b.studentid,b.result   having   b.result   > 100   ) 再试试看勒
      

  4.   

    select       name,studentid,result       from       t_record       a,T_Student       where     id=studentid   and   result       >   100       and       time       =(select       max(time)       from       t_record       b       where       a.studentid=b.studentid and  b.result       >   100      group       by       b.studentid           )   
    刚才的字查询错了,修改了下
      

  5.   

    try it ..
    select zz.studentId
      from (
            select r.studentId,
                   row_number() over(partition by r.studentId order by r.time desc) as rn,
                   r.result
              from record r,
                   student s
             where r.studentId = s.id
           )zz
     where rn = 1
       and r.result > 100;
      

  6.   

    sorry, forgot it.
    select zz.studentId
      from (
            select r.studentId,
                   row_number() over(partition by r.studentId order by r.time desc) as rn,
                   r.result
              from record r,
                   student s
             where r.studentId = s.id
           )zz
     where zz.rn = 1
       and zz.result > 100;
      

  7.   


    select name from(
    select max(time) as time,name from student s,record r  where s.id=r.studentid group by name
    ) t,record r
    where r.result>100 and r.time=t.time
    思路是
    按名字分组查出每个人最近一次考试时间然后拿最后一次考试时间和record表关连找出对应成绩单 并进行判断是否大于100
      

  8.   

    楼上的写法就很好了,提供另一种写法,拓展一下思路select * from T_Record A
    where result>100 and not exists(
       select 1 from T_Record B 
       where a.studentId = b.studentId and b.time>a.time and b.result>100
    )