{"id":3566,"date":"2023-10-27T10:41:45","date_gmt":"2023-10-27T02:41:45","guid":{"rendered":"https:\/\/white-night.club\/?p=3566"},"modified":"2023-11-09T11:07:45","modified_gmt":"2023-11-09T03:07:45","slug":"ad103","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/10\/27\/ad103\/","title":{"rendered":"1112.\u00a0\u6bcf\u4f4d\u5b66\u751f\u7684\u6700\u9ad8\u6210\u7ee9"},"content":{"rendered":"\n<p>\u8868\uff1a<code>Enrollments<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| student_id    | int     |\n| course_id     | int     |\n| grade         | int     |\n+---------------+---------+\n(student_id, course_id) \u662f\u8be5\u8868\u7684\u4e3b\u952e\uff08\u5177\u6709\u552f\u4e00\u503c\u7684\u5217\u7684\u7ec4\u5408\uff09\u3002\ngrade \u4e0d\u4f1a\u4e3a NULL\u3002<\/pre>\n\n\n\n<p>\u7f16\u5199\u89e3\u51b3\u65b9\u6848\uff0c\u627e\u51fa\u6bcf\u4f4d\u5b66\u751f\u83b7\u5f97\u7684\u6700\u9ad8\u6210\u7ee9\u548c\u5b83\u6240\u5bf9\u5e94\u7684\u79d1\u76ee\uff0c\u82e5\u79d1\u76ee\u6210\u7ee9\u5e76\u5217\uff0c\u53d6&nbsp;<code>course_id<\/code>&nbsp;\u6700\u5c0f\u7684\u4e00\u95e8\u3002\u67e5\u8be2\u7ed3\u679c\u9700\u6309&nbsp;<code>student_id<\/code>&nbsp;\u589e\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u4ee5&nbsp;<strong>\u4efb\u610f\u987a\u5e8f<\/strong>&nbsp;\u8fd4\u56de\u7ed3\u679c\u8868\u3002<\/p>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>\nEnrollments \u8868\uff1a\n+------------+-------------------+\n| student_id | course_id | grade |\n+------------+-----------+-------+\n| 2          | 2         | 95    |\n| 2          | 3         | 95    |\n| 1          | 1         | 90    |\n| 1          | 2         | 99    |\n| 3          | 1         | 80    |\n| 3          | 2         | 75    |\n| 3          | 3         | 82    |\n+------------+-----------+-------+\n<strong>\u8f93\u51fa\uff1a<\/strong>\n+------------+-------------------+\n| student_id | course_id | grade |\n+------------+-----------+-------+\n| 1          | 2         | 99    |\n| 2          | 2         | 95    |\n| 3          | 3         | 82    |\n+------------+-----------+-------+<\/pre>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u805a\u5408or\u7a97\u53e3<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u4e00\u5f00\u59cb\u7528\u7684\u805a\u5408\u51fd\u6570\u53bb\u505a\uff0c\u8bf4\u5b9e\u8bdd\u5199\u7684\u4e0d\u592a\u597d\uff0c\u521a\u521a\u597d\u5904\u4e8e\u8d85\u65f6\u7684\u90a3\u4e2a\u754c\u9650\uff0c\u6211\u63d0\u4ea4\u7b2c\u4e00\u6b21\uff0c\u80fd\u8fc7\u3002\u540c\u6837\u7684\u4ee3\u7801\u63d0\u4ea4\u7b2c\u4e8c\u6b21\u53c8\u8fc7\u4e0d\u4e86\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT student_id,MIN(course_id) AS course_id,grade\r\nFROM Enrollments AS A\r\nWHERE grade =(\r\n  SELECT MAX(grade)\r\n  FROM Enrollments AS B\r\n  WHERE A.student_id=B.student_id\r\n  GROUP BY student_id\r\n)\r\nGROUP BY student_id\r\nORDER BY student_id ASC<\/code><\/pre>\n\n\n\n<p>\u540e\u9762\u6362\u4e3a\u7528\u7a97\u53e3\u51fd\u6570\u53bb\u5199\uff0c\u65f6\u95f4\u5c11\u4e86\u4e0d\u5c11\u3002\u4e0d\u8fc7\u8bb0\u5f97\u7528row_number\uff0crank\u548cdense_rank\u90fd\u4f1a\u628a\u76f8\u540c\u503c\u7684\u6570\u636e\u6392\u5230\u540c\u4e00\u4e2arank\uff0c\u53ea\u662frank\u662f\u5426\u8fde\u7eed\u7684\u533a\u522b\u800c\u5df2\u3002<\/p>\n\n\n\n<p>\u800c\u4e14\u8981\u5728\u5f00\u7a97\u7684\u65f6\u5019\u7ed9course_id\u6392\u5e8f\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u7ed9\u7684\u6570\u636e\u8868\u672c\u8eab\u6ca1\u6392\u5e8f\uff0c\u4e0d\u77e5\u9053\u54ea\u4e2acourse_id\u662f\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Write your MySQL query statement below\r\nSELECT student_id,course_id,grade\r\nFROM (\r\n  SELECT *,\r\n  row_number()over(partition by student_id order by grade DESC,course_id ASC) AS rk \r\n  FROM Enrollments\r\n) AS t\r\nWHERE rk=1<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u805a\u5408\/\u7a97\u53e3\u51fd\u6570<\/p>\n","protected":false},"author":1,"featured_media":2827,"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\/3566"}],"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=3566"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/3566\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=3566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=3566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=3566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}