分组取最新记录的SQL

 

经常遇到这样的情况,要取得所有客户的最新交易记录,读取网站所有浏览者最后一次访问时间。一个客户只读取最新的一次记录,相同,大部分的人首先想到的就是排除所有记录,相同的只取一条。用distint,但是distint只能取到一个字段的值。所以使用distint就不能读取 出所有的情况。下面是一条正确的语句:

select   *   from   表名    自定义  
  where   时间字段=(select   max(时间字段)   from   表名   where   客户ID=自定义.客户ID)

例如:

select   *   from   GradeEmployee    a  
  where   endTime=(select   max(endTime)   from   GradeEmployee   where   employeeID=a.employeeID)

 

 

ref:

http://topic.csdn.net/u/20070811/12/e2d164bf-aada-4201-981a-bba8763e4b79.html?1217829942

http://www.groovycn.cn/html/my/200806/15-37.html

http://www.phpchina.com/viewthread_51291.html

 

 

posted @ 2008-09-19 10:44  emanlee  阅读(1443)  评论(1编辑  收藏  举报