请大家在帮忙解决的时候,请注意,如果数据量很大,假设上百万条或者上千万条记录的情况下,应保证SQL语句的执行效率尽可能优化,谢谢。

解决方案 »

  1.   

    select * from (select * from tb order by postdate desc) as a group by name
    不知道效率如何
      

  2.   

    CREATE TABLE [Table1] (  [ID] [int] NULL , [Name] [varchar] (50) NULL , [PostDate] [datetime]  NULL ) INSERT [Table1] ( [ID] , [Name] , [PostDate] ) VALUES ( 1 , 'abc' , '2002-01-01 00:00:00.000' )
     INSERT [Table1] ( [ID] , [Name] , [PostDate] ) VALUES ( 2 , 'abc' , '2002-02-01 00:00:00.000' )
     INSERT [Table1] ( [ID] , [Name] , [PostDate] ) VALUES ( 3 , 'efg' , '2002-05-01 00:00:00.000' )
     INSERT [Table1] ( [ID] , [Name] , [PostDate] ) VALUES ( 4 , 'abc' , '2002-01-20 00:00:00.000' )
     INSERT [Table1] ( [ID] , [Name] , [PostDate] ) VALUES ( 5 , 'efg' , '2002-06-12 00:00:00.000' )Select A.*,B.ID 
      From (select Distinct Name,Max(postdate) As PostDate From Table1 Group by Name) A,Table1 B 
        where A.Name = B.Name and A.postdate = B.PostDate Order by B.ID
      

  3.   

    XqYuan() :
      您的SQL语句在MYSQL中执行不了,提示是:
    ERROR 1064: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select * from tb order by postdate desc) as a group by name' a
      

  4.   

    如果是mysql5.0以上版本的话,应该没有问题
      

  5.   

    mysql默认group by是取主键第一个的数据如果先排序就按排序后的,第一个取数据
      

  6.   

    如果是SqlServer肯定不行,SqlServer和mysql group by的机制不一样
      

  7.   

    zhlmxh(梦想成真) :
    您的SQL语句在MYSQL中也执行不了,提示是:
    ERROR 1064: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select Distinct Name,Max(postdate) As PostDate From Table1 Grou
      

  8.   

    XqYuan() :
      我目前用的是MYSQL 4.0的版本,请问有其他办法吗?
      

  9.   

    晕,  这么简单,  这么多人回答都没有出来,,   看我的.select  id,distinect   name  ,postdate  from tb;这句的效率应该是最高的.
      

  10.   

    XqYuan() :
       谢谢,按照你的帮助,你的SQL语句在mysql 5.0下面完全通过,也解决了我的问题:
    select * from (select * from tb order by postdate desc) as a group by name我要测试一下这个的效率如何,呵呵,谢谢了
      

  11.   

    longenic(江寒) :
       你的SQL语法不正确,不过,也谢谢你的热心,呵呵