Mysql usage experience bitsCN.com
SET FOREIGN_KEY_CHECKS = 0;
------------------------------
-- Table structure for 'staff'
------------------------------
Drop table if exists 'staff ';
Create table 'staff '(
'Id' int (10) unsigned not null AUTO_INCREMENT,
'Name' varchar (100) default null,
'Age' smallint (10) default null,
'Department 'int (10) default null,
'Type' smallint (5) default null,
'Create _ time' datetime default null,
Primary key ('id ')
) ENGINE = MyISAM AUTO_INCREMENT = 10 default charset = utf8;
------------------------------
-- Records of staff
------------------------------
Insert into 'staff' VALUES ('1', 'Andy Lau ', '54', '1', '1', '2017-06-21 11:29:22 ');
Insert into 'staff' VALUES ('2', 'Zhang Xueyou ', '50', '1', '1', '2017-06-21 11:29:27 ');
Insert into 'staff' VALUES ('3', 'kwok City', '52 ', '1', '1', '2017-06-21 11:29:27 ');
Insert into 'employees' VALUES ('4', 'day', '53', '1', '1', '2017-06-21 11:29:27 ');
Insert into 'staff' VALUES ('5', 'Andy Lau ', '54', '2', '2', '2017-06-21 11:39:27 ');
Insert into 'staff' VALUES ('6', 'Liang Chaowei ', '55', '2', '2', '2017-06-21 11:29:27 ');
Insert into 'staff' VALUES ('7', 'Yellow Sun Hua', '57 ', '2', '2', '2017-06-21 11:29:27 ');
Insert into 'staff' VALUES ('8', 'Liang Chaowei ', '55', '3', '3', '2017-06-21 11:30:36 ');
Insert into 'staff' VALUES ('9', 'Andy Lau ', '54', '3', '3', '2017-06-21 11:31:01 ');
SELECT * FROM 'staff' as t1 group by 'name', age, department, type, create_time
Having create_time = (select max (create_time) from staff as t2 where t2.name = t1.name) # group by t2.name
;
Resolution: group by is first grouped by 'name', age, department, type, create_time. After grouping, having filters the group by create_time, finally, we get the latest record for each name, that is, the number of groups in the row to be executed how many times having create_time = (select max (create_time) from staff as t2 where t2.name = t1.name) statement, select max (create_time) from staff as t2 where t2.name = t1.name the queried data source is filtered from the entire table by name, and then find the largest one.
Think: you can replace all create_time after having with id to check the result and compare it.
Another method of implementing subquery is as follows:
SELECT * FROM
(
Select * from staff order by create_time desc
) As t1 group by 'name ';
BitsCN.com