{"id":5758,"date":"2024-11-14T13:38:53","date_gmt":"2024-11-14T05:38:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5758\/"},"modified":"2024-11-14T13:38:53","modified_gmt":"2024-11-14T05:38:53","slug":"sql%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5758\/","title":{"rendered":"sql\u8bed\u53e5\u600e\u4e48\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  \u6709\u56db\u79cd sql \u53bb\u9664\u91cd\u590d\u503c\u7684\u65b9\u6cd5\uff1a1. \u4f7f\u7528 distinct \u5173\u952e\u5b57\uff1b2. \u4f7f\u7528 group by \u5b50\u53e5\uff1b3. \u4f7f\u7528 over partition by \u5b50\u53e5\uff1b4. \u4f7f\u7528\u5b50\u67e5\u8be2\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/02\/2024060200513881976.jpg\" class=\"aligncenter\" title=\"sql\u8bed\u53e5\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" alt=\"sql\u8bed\u53e5\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL\u53bb\u9664\u91cd\u590d\u503c\u7684\u5e38\u7528\u65b9\u6cd5<\/strong><\/p>\n<p><strong>1. DISTINCT\u5173\u952e\u5b57<\/strong><\/p>\n<p>DISTINCT\u5173\u952e\u5b57\u7528\u4e8e\u4ece\u7ed3\u679c\u96c6\u4e2d\u5220\u9664\u91cd\u590d\u7684\u884c\u3002\u5b83\u5c06\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u552f\u4e00\u7684\u4e00\u884c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT DISTINCT column_name\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT DISTINCT name\nFROM students;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. GROUP BY\u5b50\u53e5<\/strong><\/p>\n<p>GROUP BY\u5b50\u53e5\u6839\u636e\u6307\u5b9a\u7684\u5217\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u53ea\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u7684\u805a\u5408\u503c\uff08\u5982COUNT\u3001SUM\uff09\u3002\u901a\u8fc7\u4f7f\u7528GROUP BY\u5b50\u53e5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6d88\u9664\u91cd\u590d\u503c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT column_name\nFROM table_name\nGROUP BY column_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT name\nFROM students\nGROUP BY name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. OVER PARTITION BY<\/strong><\/p>\n<p>OVER PARTITION BY\u5b50\u53e5\u5141\u8bb8\u6839\u636e\u6307\u5b9a\u7684\u5217\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u533a\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5206\u533a\u8ba1\u7b97\u805a\u5408\u503c\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5904\u7406\u66f4\u52a0\u590d\u6742\u7684\u53bb\u91cd\u9700\u6c42\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT column_name,\n       ROW_NUMBER() OVER (PARTITION BY column_name) AS row_num\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT name,\n       ROW_NUMBER() OVER (PARTITION BY name) AS row_num\nFROM students;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u5b50\u67e5\u8be2<\/strong><\/p>\n<p>\u5b50\u67e5\u8be2\u53ef\u4ee5\u7528\u6765\u4ece\u8868\u4e2d\u9009\u62e9\u552f\u4e00\u7684\u503c\u3002\u901a\u8fc7\u5c06\u5b50\u67e5\u8be2\u4f5c\u4e3a\u4e3b\u67e5\u8be2\u4e2d\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u53bb\u91cd\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT column_name\nFROM table_name\nWHERE column_name IN (\n    SELECT DISTINCT column_name\n    FROM table_name\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT name\nFROM students\nWHERE name IN (\n    SELECT DISTINCT name\n    FROM students\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u53bb\u91cd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u56db\u79cd sql \u53bb\u9664\u91cd\u590d\u503c\u7684\u65b9\u6cd5\uff1a1. \u4f7f\u7528 distinct \u5173\u952e\u5b57\uff1b2. \u4f7f\u7528 group by \u5b50\u53e5\uff1b3. \u4f7f\u7528 over partition by \u5b50\u53e5\uff1b4. \u4f7f\u7528\u5b50\u67e5\u8be2\u3002 SQL\u53bb\u9664\u91cd\u590d\u503c\u7684\u5e38\u7528\u65b9\u6cd5 1. DISTINCT\u5173\u952e\u5b57 DISTINCT\u5173\u952e\u5b57\u7528\u4e8e\u4ece\u7ed3\u679c\u96c6\u4e2d\u5220\u9664\u91cd\u590d\u7684\u884c\u3002\u5b83\u5c06\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u552f\u4e00\u7684\u4e00\u884c\u3002 \u8bed\u6cd5\uff1a SELECT DISTINCT column_name FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff1a SELECT DISTINCT name FROM students; \u767b\u5f55\u540e\u590d\u5236 2. GROUP BY\u5b50\u53e5 GROUP BY\u5b50\u53e5\u6839\u636e\u6307\u5b9a\u7684\u5217\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u53ea\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u7684\u805a\u5408\u503c\uff08\u5982COUNT\u3001SUM\uff09\u3002\u901a\u8fc7\u4f7f\u7528GROUP BY\u5b50\u53e5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6d88\u9664\u91cd\u590d\u503c\u3002 \u8bed\u6cd5\uff1a SELECT column_name FROM table_name GROUP BY column_name; \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff1a SELECT name FROM students GROUP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5758","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5758"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5758\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}