select  a.* , b.mc  from enterprise_zw as a inner join enterprise_jbxx as b on(a.enterprise_id=b.id) where a.publishflag = 1  order by a.publishtime desc  如果同一个b.mc有几条数据,我只想得到b.mc当中的最后一条数据.这条语句还要怎么改??

解决方案 »

  1.   

    desc 改为 asc
    select top 1 ...
      

  2.   

    select  a.* , b.mc  from enterprise_zw as a inner join enterprise_jbxx as b on(a.enterprise_id=b.id) where a.publishflag = 1  and b.id in 
    (select top 1 from enterprise_jbxx)
    order by a.publishtime
      

  3.   

    select  a.* , b.mc  from enterprise_zw as a inner join enterprise_jbxx as b on(a.enterprise_id=b.id) where a.publishflag = 1  and b.id in 
    (select top 1 id from enterprise_jbxx)
    order by a.publishtime
      

  4.   

    如果同一个b.mc有几条数据,我只想得到b.mc当中的最后一条数据.这条语句还要怎么改??
      如果不相同还是要显示
      

  5.   

    你是要求查出的b.mc 中 a.publishtime最小的记录么?用这个试一下
     select  a.* , b.mc  from enterprise_zw as a inner join enterprise_jbxx as b on(a.enterprise_id=b.id)
    where a.publishflag = 1  
    and exists(select a2.publishtime from enterprise_zw as a2 inner join enterprise_jbxx as b2 on(a2.enterprise_id=b2.id)
    where a.enterprise_id=a2.enterprise_id
    group by a2.publishtime
    having a.publishtime=min(a2.publishtime))
    order by a.publishtime desc
      

  6.   

    不会把,你看数据库求最小min(a.publishtime)是不是有很多空的阿,改动调试下
      

  7.   

    我要求查出的b.mc 中每一个的 a.publishtime最小的记录
      

  8.   

    我要求查出的是每一个b.mc的a.publishtime最小的记录
      

  9.   

    这个换了式下
    and exists(
    select b2.id from enterprise_zw as a2 inner join enterprise_jbxx as b2 on(a2.enterprise_id=b2.id)
    where b.id=b2.id
    group by b2.id
    having b.mc=min(b2.mc)
    )
      

  10.   

    我要求查出的是每一个b.mc的一条记录
      

  11.   

    晕了,每一个b.mc的一条记录那你就先查b.mc 一堆作为一个表,再去嵌套查里面你需要的什么
      

  12.   

    我要查每一个b.mc中,publishtime最大的那一条记录。真晕,看来我记的还不够清楚。
    不好意思让大家绕了这么远。。