{"id":5960,"date":"2024-11-14T08:18:59","date_gmt":"2024-11-14T00:18:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5960\/"},"modified":"2024-11-14T08:18:59","modified_gmt":"2024-11-14T00:18:59","slug":"sql%e5%a6%82%e4%bd%95%e6%9f%a5%e8%af%a2%e5%ad%97%e6%ae%b5%e9%87%8d%e5%a4%8d%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5960\/","title":{"rendered":"sql\u5982\u4f55\u67e5\u8be2\u5b57\u6bb5\u91cd\u590d\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u4f7f\u7528 sql \u7684 group by \u548c count() \u51fd\u6570\u3001distinct \u548c count() \u51fd\u6570\u6216\u7a97\u53e3\u51fd\u6570 row_number() \u6765\u67e5\u8be2\u91cd\u590d\u5b57\u6bb5\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/06\/2024060609422288279.jpg\" class=\"aligncenter\" title=\"sql\u5982\u4f55\u67e5\u8be2\u5b57\u6bb5\u91cd\u590d\u6570\u636e\u63d2\u56fe\" alt=\"sql\u5982\u4f55\u67e5\u8be2\u5b57\u6bb5\u91cd\u590d\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u67e5\u8be2\u91cd\u590d\u5b57\u6bb5\u6570\u636e<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8be2\u91cd\u590d\u5b57\u6bb5\u6570\u636e\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u67e5\u8be2\u91cd\u590d\u5b57\u6bb5\u6570\u636e\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 GROUP BY \u548c COUNT() \u51fd\u6570<\/strong><\/p>\n<pre>SELECT field_name, COUNT(*) AS count_duplicates\nFROM table_name\nGROUP BY field_name\nHAVING COUNT(*) &gt; 1<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT customer_id, COUNT(*) AS count_duplicates\nFROM orders\nGROUP BY customer_id\nHAVING COUNT(*) &gt; 1;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 DISTINCT \u548c COUNT() \u51fd\u6570<\/strong><\/p>\n<pre>SELECT field_name\nFROM table_name\nGROUP BY field_name\nHAVING COUNT(DISTINCT field_name) &lt; COUNT(*)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT customer_id\nFROM orders\nGROUP BY customer_id\nHAVING COUNT(DISTINCT customer_id) &lt; COUNT(*);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528\u7a97\u53e3\u51fd\u6570 ROW_NUMBER()<\/strong><\/p>\n<pre>SELECT field_name, ROW_NUMBER() OVER (PARTITION BY field_name ORDER BY field_name) AS row_number\nFROM table_name\nWHERE row_number &gt; 1<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT customer_id, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY customer_id) AS row_number\nFROM orders\nWHERE row_number &gt; 1;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<p>\u4e0a\u8ff0\u65b9\u6cd5\u4f7f\u7528 SQL \u51fd\u6570\u548c\u5b50\u53e5\u6765\u5206\u7ec4\u6570\u636e\u5e76\u8ba1\u7b97\u91cd\u590d\u503c\u3002GROUP BY \u5b50\u53e5\u5c06\u6570\u636e\u6309\u5b57\u6bb5\u540d\u79f0\u5206\u7ec4\uff0cCOUNT() \u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u4e2d\u91cd\u590d\u9879\u7684\u6570\u91cf\u3002HAVING \u5b50\u53e5\u7b5b\u9009\u51fa\u91cd\u590d\u9879\u8ba1\u6570\u5927\u4e8e 1 \u7684\u7ec4\u3002DISTINCT \u5173\u952e\u5b57\u53ef\u786e\u4fdd\u53ea\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u4e2d\u552f\u4e00\u503c\u3002ROW_NUMBER() \u7a97\u53e3\u51fd\u6570\u4e3a\u6bcf\u7ec4\u4e2d\u7684\u884c\u5206\u914d\u4e00\u4e2a\u987a\u5e8f\u53f7\uff0c\u5e76\u53ef\u4ee5\u7528\u6765\u8bc6\u522b\u91cd\u590d\u884c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5982\u4f55\u67e5\u8be2\u5b57\u6bb5\u91cd\u590d\u6570\u636e\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>\u53ef\u4ee5\u4f7f\u7528 sql \u7684 group by \u548c count() \u51fd\u6570\u3001distinct \u548c count() \u51fd\u6570\u6216\u7a97\u53e3\u51fd\u6570 row_number() \u6765\u67e5\u8be2\u91cd\u590d\u5b57\u6bb5\u6570\u636e\u3002 SQL \u67e5\u8be2\u91cd\u590d\u5b57\u6bb5\u6570\u636e \u95ee\u9898\uff1a\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8be2\u91cd\u590d\u5b57\u6bb5\u6570\u636e\uff1f \u56de\u7b54\uff1a \u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u67e5\u8be2\u91cd\u590d\u5b57\u6bb5\u6570\u636e\uff1a \u65b9\u6cd5 1\uff1a\u4f7f\u7528 GROUP BY \u548c COUNT() \u51fd\u6570 SELECT field_name, COUNT(*) AS count_duplicates FROM table_name GROUP BY field_name HAVING COUNT(*) &gt; 1 \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a SELECT customer_id, COUNT(*) AS count_duplicates FROM orders GROUP BY customer_id [&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-5960","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5960","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=5960"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5960\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}