這篇文章主要介紹MySQL中分組獲取前三條記錄的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
主要從事網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站建設(shè)、程序開發(fā)、微網(wǎng)站、小程序設(shè)計等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項目的能力。
要求:編寫一個SQL,獲取部門工資前三高的員工。
員工表和部門表結(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;
員工表和部門表數(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');
題庫的答案:
SELECT
d.`name` AS '部門',
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é)果如下:
部門 員工 工資
IT Max 90000
IT Randy 85000
IT Eva 85000
IT Joe 70000
Sales Henry 80000
Sales Sam 60000
首先來理解一下上面的 SQL,當(dāng) < 3 的條件改為 = 0 時,即子表中相同部門沒有比主表工資高的員工,則取得工資最高的員工;當(dāng)條件為 = 1 時,表示子表中相同部門里只有一個比主表工資高的員工,則取得工資第二高的員工;同理,條件 = 2 表示工資第三高的員工,所以工資前三高的員工的條件為 < 3。
通過結(jié)果可以看到,第二名員工和第三名員工工資相同,被當(dāng)作并列第二,并不會排擠掉第三名。如果我們希望出現(xiàn)并列第二名時,第三名就變成第四名呢?可以把 count(DISTINCT em.salary) 改成 count(*)。
SELECT
d.`name` AS '部門',
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é)果:
部門 員工 工資
IT Max 90000
IT Randy 85000
IT Eva 85000
Sales Henry 80000
Sales Sam 60000
上面的寫法中,當(dāng)我們?nèi)∏皟擅麜r,會得到 IT 部門的第一名和兩個第二名的員工。如果我們希望去掉并列的情況,即就算工資相同也分為不同名次呢?那可以根據(jù)工資排序來增加多一個序號列,把 employee 表替換成下面這個子表:
SELECT (@i:=@i+1) AS rownum, es.* FROM employee es, (select @i:=0) ri ORDER BY es.salary
1
然后去查詢每個部門工資前兩名的員工,這里注意一下,兩個子表變量名需要不一樣:
SELECT
d.`name` AS '部門',
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é)果如下:
部門 員工 工資
IT Max 90000
IT Randy 85000
Sales Henry 80000
Sales Sam 60000
以上是“mysql中分組獲取前三條記錄的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!