select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name
from a,b
where a.status=1
and a.cntid=b.id
and a.producetime in (select distinct producetime from a
                      where rownum<6
                      order by producetime desc)

解决方案 »

  1.   

    这样不行......and a.producetime in (select distinct producetime from a
                          where rownum<6
                          order by producetime desc)这个有问题.
      

  2.   

    select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name
    from a,b
    where a.status=1
    and a.cntid=b.id
    and a.producetime =(select max(producetime) from a )
      

  3.   

    select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name
    from a,b 
    where a.cntid = b.id
    and b.status = 1  
    and a.producetime in 
    (select a.producetime from a,b where a.cntid=b.id and b.status=1 having count(a.producetime)<=5 group by b.name order by a.producetime desc)
      

  4.   

    select * from (select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name from a,b where A.cntid(+)=B.id and a.status='1' order by A.producetime desc) where rownum<=5
      

  5.   

    包子开始的这个地方有问题select distinct producetime from a
                          where rownum<6
                          order by producetime desc不好意思.刚才吃饭去了.我马上看看各位的回贴,成功了马上结贴.
      

  6.   

    我看beckhambobo(beckham)的不錯,樓主用他的方法吧!
    A.cntid(+)=B.id 比較的全面
      

  7.   

    select * from (select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name from mmswebcontent a,mmswebcnttype b where a.cntid(+)=b.id and a.status=1 order by a.producetime desc) where rownum<=5;结果如下.为什么会这样?cntid=22???只有这一个的,其它的怎么没有.我的数据库里面不只有这一个类型的.        ID      CNTID
    ---------- ----------
    SUBJECT
    --------------------------------------------------------------------------------
    PREVIEWFILE          PRODUCETIM NAME
    -------------------- ---------- --------------------
           519         22
    封侯拜相
    040110xn000.jpg      17-1月 -04 新年祝福       520         22
    恭贺新禧
    040110xn001.jpg      17-1月 -04 新年祝福       521         22
    恭喜发财
    040110xn002.jpg      17-1月 -04 新年祝福       523         22
    猴年行大运
    040110xn004.jpg      17-1月 -04 新年祝福       522         22
    猴年进步
    040110xn003.jpg      17-1月 -04 新年祝福
    没了,就上面五条??我是想每个类型都按要求取出五条来.
      

  8.   

    只能建立存储过程用游标了:
    create table table_temp(
           id number(9 ,
           cntid number(4),
           subject varchar2(100),
           previewfile varchar2(20),
           status number(1),
           producetime date,
           name varchar2(20));create or replace pro_testpro
    as
           is_cntid number(4);
           cursor cur_cntid is 
           select distinct cntid from a;
    begin
           delete from table_temp;
           open cur_cntid;
           loop
                fetch cur_cntid into is_cntid;
                exit when cur_cntid%notfound;
                insert into table_temp(
                id     ,
                cntid  ,
                subject,
                previewfile,
                status ,
                producetime ,
                name   )
                select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name
                from a,b
                where a.status=1
                and a.cntid=is_cntid
                and a.cntid=b.id
                and a.producetime in (
                          select distinct producetime from a
                          where status=1
                          and   cntid=is_cntid
                          and   rownum<6
                          order by producetime desc)
           end loop;
           close cur_cntid;
    commit;
    end;
    /
                            
      

  9.   

    select * from (
    select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name,
    row_number() over (patition by cntid order by A.producetime desc) rank1
    from A,B where a.status=1 and b.status=1 and a.id=b.id
    )
    where rank1<=5
      

  10.   

    select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name from a,b where A.cntid=B.id and a.status!='1'
    union all
    select * from (select a.id,a.cntid,a.subject,a.previewfile,a.producetime,b.name from a,b where A.cntid=B.id and a.status='1' and b.status='1' order by A.producetime desc) where rownum<6