Experiences in using mysql

Source: Internet
Author: User
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

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.