本文章来给大家介绍关于mysql分组 排序取每条记录中最后更新记录,有需要了解的朋友可进入参考参考。
以下是 test 表,测试sql,代码如下:
- CREATETABLEIFNOTEXISTS`test`(
- `id`int(10)unsignedNOTNULLauto_increment,
- `install`int(10)unsignedNOTNULL,
- `day`int(10)unsignedNOTNULL,
- `aid`int(10)unsignedNOTNULL,
- PRIMARYKEY(`id`)
- )ENGINE=MyISAMDEFAULTCHARSET=utf8AUTO_INCREMENT=12;
- INSERTINTO`test`(`id`,`install`,`day`,`aid`)VALUES
- (1,1232,20080808,1),
- (2,2321,20080809,2),
- (3,1236,20080810,3),
- (5,4212,20080809,1),
- (6,2312,20080810,1),
- (7,1432,20080811,1),
- (8,2421,20080808,2),
- (9,4245,20080811,2),
- (10,5654,20080810,2),
- (11,412,20080808,3);
sql语句,代码如下:
- SELECTA.*FROMtestA,
- (SELECTaid,MAX(day)max_dayFROMtestGROUPBYaid)B
- WHEREA.aid=B.aidANDA.day=B.max_day
- ORDERBYa.installDESC
mysql实现分组排序并赋予序号的存贮过程,代码如下:
- dropprocedureset_rank;
- createprocedureset_rank()
- begin
- set@i=1;
- set@number=(selectcount(1)fromsuppliers_performance);
- updatesuppliers_performancesetscore_rank=0;
- while@i<@number
- do
- begin
- updatesuppliers_performancesetscore_rank=@iwhereidin(selectidfrom(selectidfromsuppliers_performancewherescore_rank=0orderbyscorelimit1)b);--phpfensi.com
- set@i=@i+1;
- end;
- endWHILE;
- end;