sql,联合主键,按id分组求版本号最大值的集合

时间:2021-05-15 15:11:42

表结构如下:

/*
SQLyog v10.2 
MySQL - 5.5.39 
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;

create table `seal` (
    `id` varchar (96),
    `update_user` varchar (96),
    `update_time` datetime ,
    `seal_name` varchar (150),
    `remark` varchar (600),
    `is_enable` int (1),
    `seal_image` blob ,
    `seal_version` int (11),
    `is_del` int (1)
); 
insert into `seal` (`id`, `update_user`, `update_time`, `seal_name`, `remark`, `is_enable`, `seal_image`, `seal_version`, `is_del`) values('91b2b13c47aa4cf7b5faf9093c899cfc','820e2deb8efe11e4b4a2bcaec5330601','2015-11-26 15:53:59','1','22222','1','??,'1','1');
insert into `seal` (`id`, `update_user`, `update_time`, `seal_name`, `remark`, `is_enable`, `seal_image`, `seal_version`, `is_del`) values('91b2b13c47aa4cf7b5faf9093c899cfc','820e2deb8efe11e4b4a2bcaec5330601','2015-11-26 15:54:49','1','1111','1','塒NG\r\n\Z\n','2','1');

 

对应的mysql如下:

sql,联合主键,按id分组求版本号最大值的集合

 

现在的需求是:因为表中用的是联合主键,id+seal_version,所以id可能相同,

要求:求出id一样的数据中,seal_version最大的数据

select * from seal sl
left join
(
select id,max(seal_version) as seal_version from seal
group by id
) temp
on sl.id=temp.id and sl.seal_version=temp.version