查詢員工表倒數第三入職日期的所有員工,要考慮到有同一天入職的員工,所以使用去重先查出倒數第三日期,再使用這個日期去查詢所有相等的員工資訊
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'
);
本作品採用《CC 協議》,轉載必須註明作者和本文連結