{"id":4778,"date":"2023-11-10T10:44:35","date_gmt":"2023-11-10T02:44:35","guid":{"rendered":"https:\/\/white-night.club\/?p=4778"},"modified":"2023-11-10T23:33:26","modified_gmt":"2023-11-10T15:33:26","slug":"ad142","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/11\/10\/ad142\/","title":{"rendered":"618.\u00a0\u5b66\u751f\u5730\u7406\u4fe1\u606f\u62a5\u544a"},"content":{"rendered":"\n<p>\u8868\uff1a&nbsp;<code>student<\/code>&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| name        | varchar |\n| continent   | varchar |\n+-------------+---------+\n\u8be5\u8868\u53ef\u80fd\u5305\u542b\u91cd\u590d\u7684\u884c\u3002\n\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u8868\u793a\u5b66\u751f\u7684\u540d\u5b57\u548c\u4ed6\u4eec\u6765\u81ea\u7684\u5927\u9646\u3002\n<\/pre>\n\n\n\n<p>\u4e00\u6240\u5b66\u6821\u6709\u6765\u81ea\u4e9a\u6d32\u3001\u6b27\u6d32\u548c\u7f8e\u6d32\u7684\u5b66\u751f\u3002<\/p>\n\n\n\n<p>\u7f16\u5199\u89e3\u51b3\u65b9\u6848\u5b9e\u73b0\u5bf9\u5927\u6d32\uff08continent\uff09\u5217\u7684&nbsp;<a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E9%80%8F%E8%A7%86%E8%A1%A8\" target=\"_blank\" rel=\"noreferrer noopener\">\u900f\u89c6\u8868<\/a>&nbsp;\u64cd\u4f5c\uff0c\u4f7f\u5f97\u6bcf\u4e2a<code>\u5b66\u751f<\/code>\u6309\u7167\u59d3\u540d\u7684<strong>\u5b57\u6bcd\u987a\u5e8f<\/strong>\u4f9d\u6b21\u6392\u5217\u5728\u5bf9\u5e94\u7684\u5927\u6d32\u4e0b\u9762\u3002\u8f93\u51fa\u7684\u6807\u9898\u5e94\u4f9d\u6b21\u4e3a<code>\u7f8e\u6d32\uff08America\uff09\u3001\u4e9a\u6d32\uff08Asia\uff09\u548c\u6b27\u6d32\uff08Europe\uff09\u3002<\/code><\/p>\n\n\n\n<p>\u6d4b\u8bd5\u7528\u4f8b\u7684\u751f\u6210\u4fdd\u8bc1\u6765\u81ea\u7f8e\u56fd\u7684\u5b66\u751f\u4eba\u6570\u4e0d\u5c11\u4e8e\u4e9a\u6d32\u6216\u6b27\u6d32\u7684\u5b66\u751f\u4eba\u6570\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u6240\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong> \nStudent table:\n+--------+-----------+\n| name   | continent |\n+--------+-----------+\n| Jane   | America   |\n| Pascal | Europe    |\n| Xi     | Asia      |\n| Jack   | America   |\n+--------+-----------+\n<strong>\u8f93\u51fa:<\/strong> \n+---------+------+--------+\n| America | Asia | Europe |\n+---------+------+--------+\n| Jack    | Xi   | Pascal |\n| Jane    | null | null   |\n+---------+------+--------+<\/pre>\n\n\n\n<p><strong>\u8fdb\u9636\uff1a<\/strong>\u5982\u679c\u4e0d\u80fd\u786e\u5b9a\u54ea\u4e2a\u5927\u6d32\u7684\u5b66\u751f\u6570\u6700\u591a\uff0c\u4f60\u53ef\u4ee5\u5199\u51fa\u4e00\u4e2a\u67e5\u8be2\u53bb\u751f\u6210\u4e0a\u8ff0\u5b66\u751f\u62a5\u544a\u5417\uff1f<\/p>\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>\u900f\u89c6\u8868+\u6d3e\u751f\u8868+MAX<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u6709\u70b9\u62bd\u8c61\u3002<\/p>\n\n\n\n<p>\u5148\u662f\u884c\u8f6c\u5217\u3002\u6211\u8fd8\u6ca1\u5b66\u5230PIVOT\u90a3\u5757\uff0c\u4e0d\u8fc7\u7528CASE WHEN\u4e5f\u80fd\u5b9e\u73b0\u884c\u8f6c\u5217\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u5148\u5bf9\u5404\u5927\u9646\u7684\u5b66\u751frow_number\u6392\u5e8f\uff0c\u518d\u6309id\u5206\u7ec4\u7684\u8bdd\u5c31\u80fd\u5f97\u5230\u6bcf\u4e00\u884c\u7684\u5b66\u751f\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u8f93\u51fa\u7ed3\u679c\u662f\u4e09\u5217\uff0c\u76f8\u5f53\u4e8e\u5143\u7ec4\u53d6\u503c\u662f\uff08name\uff0cnull\uff0cnull\uff09~\uff08name\uff0cname\uff0cname\uff09\u3002<\/p>\n\n\n\n<p>\u518d\u7528MAX\u53d6\u6700\u5927\u503c\u3002\u8bf4\u5b9e\u8bdd\u8fd9\u4e2a\u6211\u662f\u771f\u6ca1\u60f3\u5230\u3002\u5982\u679c\u5143\u7ec4\u662f\uff08name\uff0cnull\uff0cnull\uff09\uff0c\u90a3\u4e48\u7528max\u53bb\u7a7a\u4e4b\u540e\u5c31\u80fd\u5f97\u5230name\u7684\u503c\u3002\u5982\u679c\u5143\u7ec4\u662f\uff08name\uff0cname\uff0cname\uff09\uff0c\u90a3\u4e48\u4e5f\u662f\u7528max\u6839\u636e\u8fd9\u4e2aname\u5bf9\u5e94\u7684continent\u518d\u53bb\u9009\u51fa\u5bf9\u5e94\u7684name\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Write your MySQL query statement below\nSELECT \nMAX(CASE WHEN continent='America' THEN name END) AS America,\nMAX(CASE WHEN continent='Asia' THEN name END) AS Asia,\nMAX(CASE WHEN continent='Europe' THEN name END) AS Europe\nFROM (\n  SELECT name,continent,\n  row_number()over(partition by continent order by name ASC) AS rk \n  FROM Student\n)t\nGROUP BY t.rk\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u900f\u89c6\u8868\uff0b\u6d3e\u751f\u8868+MAX<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"closed","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\/4778"}],"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=4778"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/4778\/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=4778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=4778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=4778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}