{"id":7989,"date":"2024-04-02T11:20:25","date_gmt":"2024-04-02T03:20:25","guid":{"rendered":"https:\/\/white-night.club\/?p=7989"},"modified":"2024-04-02T11:38:38","modified_gmt":"2024-04-02T03:38:38","slug":"ad173","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2024\/04\/02\/ad173\/","title":{"rendered":"185. \u90e8\u95e8\u5de5\u8d44\u524d\u4e09\u9ad8\u7684\u6240\u6709\u5458\u5de5"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/department-top-three-salaries\/\">https:\/\/leetcode.cn\/problems\/department-top-three-salaries\/<\/a><\/p>\n\n\n\n<p>\u4e00\u5f00\u59cb\u4e0b\u610f\u8bc6\u7528\u7684LEFT JOIN\uff0c\u4f46\u53d1\u73b0\u7b2c18\u4e2a\u6837\u4f8b\u8fc7\u4e0d\u53bb\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"329\" height=\"427\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2024\/04\/image-1.png\" alt=\"\" class=\"wp-image-7993\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2024\/04\/image-1.png 329w, https:\/\/white-night.club\/wp-content\/uploads\/2024\/04\/image-1-231x300.png 231w\" sizes=\"(max-width: 329px) 100vw, 329px\" \/><\/figure>\n\n\n\n<p>\u53d1\u73b0\u591a\u4e86null\u3002\u56e0\u4e3aLEFT JOIN\u5982\u679c\u627e\u4e0d\u5230\u8bb0\u5f55\u5c31\u4f1a\u5728\u88abLEFT JOIN\u7684\u8bb0\u5f55\u65c1\u8fb9\u52a0\u4e0anull\uff0c\u4e5f\u5c31\u662f\u201dLEFT\u4ea7null\u201c\u3002\u90a3\u628aLEFT JOIN\u6539\u6210JOIN\u6216\u8005\u53bb\u6389LEFT\u5c31\u5b8c\u4e8b\u4e86\u3002\u518d\u8dd1\u4e00\u904d\u770b\u770b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"334\" height=\"436\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2024\/04\/image.png\" alt=\"\" class=\"wp-image-7990\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2024\/04\/image.png 334w, https:\/\/white-night.club\/wp-content\/uploads\/2024\/04\/image-230x300.png 230w\" sizes=\"(max-width: 334px) 100vw, 334px\" \/><\/figure>\n\n\n\n<p>dense_rank\u4e5f\u53ea\u80fd\u6d3e\u751f\u8868\u7528\u4e86\uff0c\u5916\u9762\u7528\u7684\u8bdd\u4f1a\u591a\u4e00\u5217\u6392\u5e8f\u3002\u4e0d\u8fc7\u4e0d\u7528\u7a97\u53e3\u51fd\u6570\u7684\u8bdd\uff0c\u4f30\u8ba1\u662fGROUP BY+ORDER BY\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">SQL<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Write your MySQL query statement below\nSELECT A.Department,A.Employee,A.Salary\nFROM(\n    SELECT Department.name AS Department,Employee.name AS Employee,Employee.salary AS Salary,\n    dense_rank()over(partition by Employee.departmentId ORDER BY Employee.salary DESC) as rk\n    FROM Department\n    JOIN Employee \n    ON Department.id=Employee.departmentId\n)AS A \nWHERE A.rk&lt;=3\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\"># Write your MySQL query <\/span><span style=\"color: #569CD6\">statement<\/span><span style=\"color: #D4D4D4\"> below<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">SELECT<\/span><span style=\"color: #D4D4D4\"> A.Department,A.Employee,A.Salary<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">FROM<\/span><span style=\"color: #D4D4D4\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">SELECT<\/span><span style=\"color: #D4D4D4\"> Department.name <\/span><span style=\"color: #569CD6\">AS<\/span><span style=\"color: #D4D4D4\"> Department,Employee.name <\/span><span style=\"color: #569CD6\">AS<\/span><span style=\"color: #D4D4D4\"> Employee,Employee.salary <\/span><span style=\"color: #569CD6\">AS<\/span><span style=\"color: #D4D4D4\"> Salary,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">dense_rank<\/span><span style=\"color: #D4D4D4\">()<\/span><span style=\"color: #569CD6\">over<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">partition<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">by<\/span><span style=\"color: #D4D4D4\"> Employee.departmentId <\/span><span style=\"color: #569CD6\">ORDER BY<\/span><span style=\"color: #D4D4D4\"> Employee.salary <\/span><span style=\"color: #569CD6\">DESC<\/span><span style=\"color: #D4D4D4\">) <\/span><span style=\"color: #569CD6\">as<\/span><span style=\"color: #D4D4D4\"> rk<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">FROM<\/span><span style=\"color: #D4D4D4\"> Department<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">JOIN<\/span><span style=\"color: #D4D4D4\"> Employee <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">ON<\/span><span style=\"color: #D4D4D4\"> Department.id=Employee.departmentId<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">)<\/span><span style=\"color: #569CD6\">AS<\/span><span style=\"color: #D4D4D4\"> A <\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">WHERE<\/span><span style=\"color: #D4D4D4\"> A.rk&lt;=<\/span><span style=\"color: #B5CEA8\">3<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d3e\u751f\u8868+dense_rank+JOIN<\/p>\n","protected":false},"author":1,"featured_media":7990,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/7989"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=7989"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/7989\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/7990"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=7989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=7989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=7989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}