select a.title,a.intime,b.typename,c.postiltime from document a,type b,postil c where a.typeid=b.typeid and a.documentid=c.documentid and a.pinst_id = 2  这条SQL是正确的可是select a.title,a.intime,b.typename,MAX(c.postiltime) from document a,type b,postil c where a.typeid=b.typeid and a.documentid=c.documentid and a.pinst_id = 2 为什么就报错呢如果MAX不能用,那我怎么做才能取到c.postiltime的最上面一条记录呢(注:ORACLE 库,c.postiltime字段是VARCHAR2,里面放的是表示时间的字符串 )

解决方案 »

  1.   

    select a.title,a.intime,b.typename,c.postiltime from document a,type b,postil c where a.typeid=b.typeid and a.documentid=c.documentid and a.pinst_id = 2 and c.postiltime = MAX(c.postiltime)
    要一条的话
    select top 1 a.title,a.intime,b.typename,c.postiltime from document a,type b,postil c where a.typeid=b.typeid and a.documentid=c.documentid and a.pinst_id = 2 order by c.postiltime desc
      

  2.   

    使用到聚合函数(如Max等),如果还Select出其他列,务必要Group其他列,即以其他列分组.select a.title,a.intime,b.typename,MAX(c.postiltime) from document a,type b,postil c where a.typeid=b.typeid and a.documentid=c.documentid and a.pinst_id = 2 
    group by a.title,a.intime,b.typename
      

  3.   

    是不是 c.postiltime 字段需要转换一下ORACLE:没用过  
    Sql server中:
    select max(convert(char,c.postiltime,11)) from....
      

  4.   

    select a.title,a.intime,b.typename,MAX(c.postiltime)
     from document a,type b,postil c 
    where a.typeid=b.typeid and a.documentid=c.documentid and a.pinst_id = 2 group by a.title,a.intime,b.typename
      

  5.   

    happyfamily(FreeHorse) ( )
    俺替楼主感谢你.
      

  6.   

    agree with iuhxq(小灰(http://blog.csdn.net/iuhxq))
      

  7.   

    ORACLE 库用top好像不行吧,happyfamily(的方法报语法错
      

  8.   

    select a.title,a.intime,b.typename,MAX(c.postiltime) from document a
    left join type b on a.typeid=b.typeid
    left join postil c on a.documentid=c.documentid
    where  a.pinst_id = 2 
    group by a.title,a.intime,b.typename
      

  9.   

    ect a.title,a.intime,b.typename,c.postiltime from document a,type b,postil c where a.typeid=b.typeid and a.documentid=c.documentid and a.pinst_id = 2 and c.postiltime = MAX(c.postiltime)
    要一条的话
    select top 1 a.title,a.intime,b.typename,c.postiltime from document a,type b,postil c where a.typeid=b.typeid and a.documentid=c.documentid and a.pinst_id = 2 order by c.postiltime desc说得对