您好,登錄后才能下訂單哦!
這篇文章主要介紹mysql中分組獲取前三條記錄的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
要求:編寫(xiě)一個(gè)SQL,獲取部門(mén)工資前三高的員工。
員工表和部門(mén)表結(jié)構(gòu):
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255),
`salary` decimal(10,2),
`department_id` int(11),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
員工表和部門(mén)表數(shù)據(jù):
INSERT INTO `employee`(`id`, `name`, `salary`, `department_id`) VALUES (1, 'Joe', 70000.00, 1);
INSERT INTO `employee`(`id`, `name`, `salary`, `department_id`) VALUES (2, 'Henry', 80000.00, 2);
INSERT INTO `employee`(`id`, `name`, `salary`, `department_id`) VALUES (3, 'Sam', 60000.00, 2);
INSERT INTO `employee`(`id`, `name`, `salary`, `department_id`) VALUES (4, 'Max', 90000.00, 1);
INSERT INTO `employee`(`id`, `name`, `salary`, `department_id`) VALUES (5, 'Janet', 69000.00, 1);
INSERT INTO `employee`(`id`, `name`, `salary`, `department_id`) VALUES (6, 'Randy', 85000.00, 1);
INSERT INTO `employee`(`id`, `name`, `salary`, `department_id`) VALUES (7, 'Eva', 85000.00, 1);
INSERT INTO `department`(`id`, `name`) VALUES (1, 'IT');
INSERT INTO `department`(`id`, `name`) VALUES (2, 'Sales');
題庫(kù)的答案:
SELECT
d.`name` AS '部門(mén)',
e.`name` AS '員工',
e.salary AS '工資'
FROM
employee e
JOIN department d ON d.id = e.department_id
WHERE
(
SELECT count(DISTINCT em.salary) FROM employee em WHERE em.salary > e.salary AND em.department_id = e.department_id
) < 3
ORDER BY e.department_id, e.salary DESC
輸出結(jié)果如下:
部門(mén) 員工 工資
IT Max 90000
IT Randy 85000
IT Eva 85000
IT Joe 70000
Sales Henry 80000
Sales Sam 60000
首先來(lái)理解一下上面的 SQL,當(dāng) < 3 的條件改為 = 0 時(shí),即子表中相同部門(mén)沒(méi)有比主表工資高的員工,則取得工資最高的員工;當(dāng)條件為 = 1 時(shí),表示子表中相同部門(mén)里只有一個(gè)比主表工資高的員工,則取得工資第二高的員工;同理,條件 = 2 表示工資第三高的員工,所以工資前三高的員工的條件為 < 3。
通過(guò)結(jié)果可以看到,第二名員工和第三名員工工資相同,被當(dāng)作并列第二,并不會(huì)排擠掉第三名。如果我們希望出現(xiàn)并列第二名時(shí),第三名就變成第四名呢?可以把 count(DISTINCT em.salary) 改成 count(*)。
SELECT
d.`name` AS '部門(mén)',
e.`name` AS '員工',
e.salary AS '工資'
FROM
employee e
JOIN department d ON d.id = e.department_id
WHERE
(
SELECT count(*) FROM employee em WHERE em.salary > e.salary AND em.department_id = e.department_id
) < 3
ORDER BY e.department_id, e.salary DESC
輸出結(jié)果:
部門(mén) 員工 工資
IT Max 90000
IT Randy 85000
IT Eva 85000
Sales Henry 80000
Sales Sam 60000
上面的寫(xiě)法中,當(dāng)我們?nèi)∏皟擅麜r(shí),會(huì)得到 IT 部門(mén)的第一名和兩個(gè)第二名的員工。如果我們希望去掉并列的情況,即就算工資相同也分為不同名次呢?那可以根據(jù)工資排序來(lái)增加多一個(gè)序號(hào)列,把 employee 表替換成下面這個(gè)子表:
SELECT (@i:=@i+1) AS rownum, es.* FROM employee es, (select @i:=0) ri ORDER BY es.salary
1
然后去查詢每個(gè)部門(mén)工資前兩名的員工,這里注意一下,兩個(gè)子表變量名需要不一樣:
SELECT
d.`name` AS '部門(mén)',
e.`name` AS '員工',
e.salary AS '工資'
FROM
(SELECT (@i:=@i+1) AS rownum, es.* FROM employee es, (select @i:=0) ri ORDER BY es.salary) e
JOIN department d ON d.id = e.department_id
WHERE
(
SELECT count(*) FROM (SELECT (@j:=@j+1) AS rownum, es.* FROM employee es, (select @j:=0) rj ORDER BY es.salary) em WHERE em.rownum > e.rownum AND em.department_id = e.department_id
) < 2
ORDER BY e.department_id, e.salary DESC
結(jié)果如下:
部門(mén) 員工 工資
IT Max 90000
IT Randy 85000
Sales Henry 80000
Sales Sam 60000
以上是“mysql中分組獲取前三條記錄的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。