查询员工表倒数第三入职日期的所有员工,要考虑到有同一天入职的员工,所以使用去重先查出倒数第三日期,再使用这个日期去查询所有相等的员工信息
SELECT *FROM `employees`WHERE `hire_date` = ( SELECT DISTINCT `hire_date` FROM `employees` ORDER BY `hire_date` DESC LIMIT 2, 1)
CREATE
drop table if exists `employees`;CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_date` date NOT NULL,`first_name` varchar(14) NOT NULL,`last_name` varchar(16) NOT NULL,`gender` char(1) NOT NULL,`hire_date` date NOT NULL,)
INSERT
INSERT INTO `employees` ( `emp_no`, `birth_date`, `first_name`, `last_name`, `gender`, `hire_date`)VALUES ( 1, '2021-08-04', 'Georgi', 'Facello', 'M', '1980-06-21' );INSERT INTO `employees` ( `emp_no`, `birth_date`, `first_name`, `last_name`, `gender`, `hire_date`)VALUES ( 2, '2021-08-20', 'Bezalel', 'Simnel', 'F', '1985-11-21' );INSERT INTO `employees` ( `emp_no`, `birth_date`, `first_name`, `last_name`, `gender`, `hire_date`)VALUES ( 3, '2021-08-20', 'Parto', 'Bamford', 'M', '1986-08-28' );INSERT INTO `employees` ( `emp_no`, `birth_date`, `first_name`, `last_name`, `gender`, `hire_date`)VALUES ( 4, '2021-08-20', 'Chirstian', 'Koblick', 'M', '1986-12-01' );
推荐学习:《mysql视频教程》