SQL, Federated primary Key, group by ID to find a collection of the maximum version number

Source: Internet
Author: User

The table structure is as follows:

/*sqlyog v10.2 MySQL-5.5.39 **********************************************************************//*!40101 SET NAMES UTF8*/;Create Table' seal ' (' ID ')varchar( the), ' Update_user 'varchar( the), ' Update_time 'datetime, ' Seal_name 'varchar( Max), ' remark 'varchar( -), ' is_enable 'int(1), ' seal_image ' blob, ' seal_version 'int( One), ' 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',' -- One- -  the: Wu: the','1','1111','1','塒 ng\r\n\z\n','2','1');

The corresponding MySQL is as follows:

The demand now is: because the table uses the Federated primary key, Id+seal_version, so the ID may be the same,

Requirements: To find the same data as the ID, seal_version the largest data

 select  *    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 

SQL, Federated primary Key, group by ID to find a collection of the maximum version number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.