表结构为:
itemd-no  itemd-repply  itemd-date                        
1         好              2005-08-29 
1         好              2007-02-29 
1         好              2008-09-28 
2         真的            2007-09-29 
2         真的            2006-09-29
3         大家好          2008-10-02 想查询出来itemd-no相同的记录里面更新日期最近的一条,上面的查询结果为;
itemd-no  itemd-repply  itemd-date 
1         好              2008-09-28 
2         真的            2007-09-29
3         大家好          2008-10-02 
SQL语句怎么写啊,真的很难吗?能不能写一条SQl实现啊。

解决方案 »

  1.   

    唉,按说一般性问题应该到专门的csdn论坛里问的。简单提示一下:分组(group by),然后在select字段表达式上使用统计函数返回最大的日期。
      

  2.   

    select * from table1 where itemd-date in (select max(itemd-date) from table1 group by  itemd-repply)
      

  3.   

    select itemd-no,itemd-repply, max(itemd-date )from  table group by itemd-no,itemd-repply;
    查询结果为
    1        好              2008-09-28 
    2        真的            2007-09-29 
    当表中记录为一条时,不能查询出来,怎么办,请高手帮忙?
           
      

  4.   

    Group by不应该有 itemd-no
      

  5.   

    设,上面的表为表A,下面的表为表Bsql语句如下:SELECT * FROM A , B WHERE A.itemd-no = b.itemd-no order by id,itemd-date, ASC
      

  6.   

    可以
    实现你这个需求所面对的障碍是没有自增列(或联合主键) 但可以通过表自身的连接查询实现类似联合主键的功能
    设表名为test 则SQL如下select * from test t1,(select itemd-no,max(itemd-datemgpid) as maxDate from test group by itemd-no) as t2 where t1.itemd-no=t2.itemd-no and t1.itemd-datamgpid=t2.maxDate order by t1.itemd-no asc不过还是建议加一自增列 处理更简单
      

  7.   

    select * from T a where not exists(select 1 from T where [itemd-no]=a.[itemd-no] 
    and [itemd-date]>a.[itemd-date])
    /*
    itemd-no    itemd-repply itemd-date
    ----------- ------------ ----------
    1           好            2008-09-28
    2           真的           2007-09-29
    3           大家好          2008-10-02
    */