MySQL query in each grouping latest piece of data

Development often encounter, the latest data packet inquiries, such as below this table (each address query the latest record):

sql as follows:

-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `address` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `create_time` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` The VALUES ( 1 , " Zhang 1 ' , ' Beijing ' , ' 2019-09-10 11:22:23 ' );
 the INSERT  the INTO ` test` the VALUES ( 2 , ' John Doe 2 ' , ' Beijing ' , ' 2019- 12:22:23 09-10 ' );
 the INSERT  the INTO `test` the VALUES ( 3 , ' Zhang 3 ' , ' Beijing ' , ' 2019-09-05 12:22:23' );
 The INSERT  the INTO `test` the VALUES ( 4 , ' John Doe 4 ' , ' Beijing ' , ' 2019-09-06 12:22:23 ' );
 the INSERT  the INTO ` test` the VALUES ( . 5 , ' John Doe 1 ' , ' Shanghai ' , ' 2019-09-06 12:22:23 ' );
 the INSERT  the INTO `test` the VALUES ( . 6 , ' John Doe 2 ' , 'Shanghai ' , ' 2019-09-07 12:22:23 ' );
 the INSERT  the INTO `test` the VALUES ( . 7 , ' John Doe 3 ' , ' Shanghai ' , ' 2019-09-11 12:22:23 ' );
 the INSERT  the INTO `test` the VALUES ( . 8 , ' John Doe 4 ' , ' Shanghai ' , ' 2019-09-12 12:22:23 ' );
 the INSERT  the INTO ` test` the VALUES ( . 9, ' King 1 ' , ' Guangzhou ' , ' 2019-09-03 12:22:23 ' );
 the INSERT  the INTO `test` the VALUES ( 10 , ' the king 2 ' , ' Guangzhou ' , ' 2019-09-04 12:22:23 ' );
 the INSERT  the INTO `test` the VALUES ( . 11 , ' the king 3 ' , ' Guangzhou ' , ' 2019-09-05 12:22:23 ' );

 We will usually be arranged in chronological flashbacks then be grouped for the latest record for each address, sql as follows:

SELECT * FROM(SELECT * FROM test ORDER BY create_time DESC) a GROUP BY address

But the results are not what we want:

 

 

 Arranged according to the execution time flashback results:

 

So the results really want to get the record id is 2/8/11, the above query get is 1/5/9, this is why?

Because in mysql5.7, sorted subquery has become invalid, probably because most of the subquery to the main query as a result of use, the sub-query does not need the sort of reasons.

So how should we investigate it, there are two ways:

The first:

SELECT * FROM(SELECT * FROM test ORDER BY create_time DESC LIMIT 10000) a GROUP BY address

The results are:

Sort the subquery limit restrictions so that the child can not just sort query, so at this sort it will take effect, but it can only be set to limit the number of possible bigger

The second:

SELECT t.* FROM (SELECT address,max(create_time) as create_time FROM test GROUP BY address) a LEFT JOIN test t ON t.address=a.address and t.create_time=a.create_time

And address to obtain the latest time (as required according to address packets) through the MAX function, followed by a table and associated query as the original data,

Conditions and time to address that and get the maximum time and address are equal, then the result is:

 

 Efficiency, there is no comparison, continued ...

 

Guess you like

Origin www.cnblogs.com/java-spring/p/11498457.html