{"id":5876,"date":"2024-11-14T12:19:26","date_gmt":"2024-11-14T04:19:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5876\/"},"modified":"2024-11-14T12:19:26","modified_gmt":"2024-11-14T04:19:26","slug":"sql%e9%87%8d%e5%a4%8d%e8%ae%b0%e5%bd%95%e6%80%8e%e4%b9%88%e5%8e%bb%e6%8e%89","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5876\/","title":{"rendered":"sql\u91cd\u590d\u8bb0\u5f55\u600e\u4e48\u53bb\u6389"},"content":{"rendered":"<blockquote><p>\n  sql\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u67095\u79cd\u5e38\u7528\u65b9\u6cd5\uff1adistinct\u5173\u952e\u5b57\uff0c\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u552f\u4e00\u8bb0\u5f55\uff1bgroup by\u5b50\u53e5\uff0c\u5206\u7ec4\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u4e0d\u91cd\u590d\u7684\u8bb0\u5f55\uff1bunion all\u548cdistinct\uff0c\u5408\u5e76\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u5e76\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\uff1bnot in\u5b50\u67e5\u8be2\uff0c\u4ece\u4e00\u4e2a\u8868\u4e2d\u53bb\u9664\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u91cd\u590d\u7684\u8bb0\u5f55\uff1bcte\uff08\u516c\u5171\u8868\u8868\u8fbe\u5f0f\uff09\uff0c\u4f7f\u7528 row_number() \u51fd\u6570\u5206\u914d\u552f\u4e00\u5e8f\u53f7\u6765\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060321302517826.jpg\" class=\"aligncenter\" title=\"sql\u91cd\u590d\u8bb0\u5f55\u600e\u4e48\u53bb\u6389\u63d2\u56fe\" alt=\"sql\u91cd\u590d\u8bb0\u5f55\u600e\u4e48\u53bb\u6389\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u662f\u4e00\u79cd\u5e38\u89c1\u9700\u6c42\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u8d28\u91cf\u548c\u6027\u80fd\u3002\u8fd9\u91cc\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u552f\u4e00\uff08\u4e0d\u91cd\u590d\uff09\u8bb0\u5f55\u3002\u5b83\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5173\u952e\u5b57\u4e00\u8d77\u4f7f\u7528\uff0c\u4f8b\u5982 SELECT\u3001FROM \u548c WHERE\u3002<\/p>\n<pre>SELECT DISTINCT column_name\nFROM table_name;<\/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\u5c06\u67e5\u8be2\u7ed3\u679c\u5206\u7ec4\uff0c\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u4e0d\u91cd\u590d\u7684\u8bb0\u5f55\u3002\u53ef\u4ee5\u7ed3\u5408\uff08\u4f8b\u5982 COUNT\u3001SUM\uff09\u4f7f\u7528\u3002<\/p>\n<pre>SELECT column_name, COUNT(*) AS count\nFROM table_name\nGROUP BY column_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. UNION ALL \u548c DISTINCT<\/strong><\/p>\n<p>UNION ALL \u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u4e3a\u4e00\u4e2a\u7ed3\u679c\u96c6\u3002\u7ed3\u5408 DISTINCT \u53ef\u4ee5\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u3002<\/p>\n<pre>SELECT * FROM table_name\nUNION ALL\nSELECT * FROM table_name2\nDISTINCT;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. NOT IN \u5b50\u67e5\u8be2<\/strong><\/p>\n<p>NOT IN \u5b50\u67e5\u8be2\u53ef\u4ee5\u4ece\u4e00\u4e2a\u8868\u4e2d\u53bb\u9664\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u8bb0\u5f55\u91cd\u590d\u7684\u8bb0\u5f55\u3002<\/p>\n<pre>SELECT * FROM table_name\nWHERE column_name NOT IN (SELECT column_name FROM other_table);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. CTE\uff08\u516c\u5171\u8868\u8868\u8fbe\u5f0f\uff09<\/strong><\/p>\n<p>CTE \u662f\u4e00\u79cd\u4e34\u65f6\u8868\uff0c\u53ef\u4ee5\u5728\u67e5\u8be2\u4e2d\u591a\u6b21\u5f15\u7528\u3002\u53ef\u4ee5\u4f7f\u7528 ROW_NUMBER() \u51fd\u6570\u4e3a\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u5e8f\u53f7\uff0c\u7136\u540e\u4f7f\u7528\u5b83\u6765\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u3002<\/p>\n<pre>WITH RankedData AS (\n    SELECT *, ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY id) AS row_num\n    FROM table_name\n)\nSELECT * FROM RankedData\nWHERE row_num = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u96c6\u548c\u67e5\u8be2\u7684\u5177\u4f53\u8981\u6c42\u3002\u5bf9\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u96c6\uff0cDISTINCT \u5173\u952e\u5b57\u6216 GROUP BY \u5b50\u53e5\u901a\u5e38\u5c31\u8db3\u591f\u4e86\u3002\u5bf9\u4e8e\u8f83\u5927\u7684\u6570\u636e\u96c6\uff0cCTE \u6216 NOT IN \u5b50\u67e5\u8be2\u53ef\u80fd\u66f4\u6709\u6548\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u91cd\u590d\u8bb0\u5f55\u600e\u4e48\u53bb\u6389\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>sql\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u67095\u79cd\u5e38\u7528\u65b9\u6cd5\uff1adistinct\u5173\u952e\u5b57\uff0c\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u552f\u4e00\u8bb0\u5f55\uff1bgroup by\u5b50\u53e5\uff0c\u5206\u7ec4\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u4e0d\u91cd\u590d\u7684\u8bb0\u5f55\uff1bunion all\u548cdistinct\uff0c\u5408\u5e76\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u5e76\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\uff1bnot in\u5b50\u67e5\u8be2\uff0c\u4ece\u4e00\u4e2a\u8868\u4e2d\u53bb\u9664\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u91cd\u590d\u7684\u8bb0\u5f55\uff1bcte\uff08\u516c\u5171\u8868\u8868\u8fbe\u5f0f\uff09\uff0c\u4f7f\u7528 row_number() \u51fd\u6570\u5206\u914d\u552f\u4e00\u5e8f\u53f7\u6765\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u3002 SQL \u4e2d\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5 \u5728 SQL \u4e2d\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u662f\u4e00\u79cd\u5e38\u89c1\u9700\u6c42\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u8d28\u91cf\u548c\u6027\u80fd\u3002\u8fd9\u91cc\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff1a 1. DISTINCT \u5173\u952e\u5b57 DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u552f\u4e00\uff08\u4e0d\u91cd\u590d\uff09\u8bb0\u5f55\u3002\u5b83\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5173\u952e\u5b57\u4e00\u8d77\u4f7f\u7528\uff0c\u4f8b\u5982 SELECT\u3001FROM \u548c WHERE\u3002 SELECT DISTINCT column_name FROM table_name; \u767b\u5f55\u540e\u590d\u5236 2. GROUP BY \u5b50\u53e5 GROUP BY \u5b50\u53e5\u5c06\u67e5\u8be2\u7ed3\u679c\u5206\u7ec4\uff0c\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u4e0d\u91cd\u590d\u7684\u8bb0\u5f55\u3002\u53ef\u4ee5\u7ed3\u5408\uff08\u4f8b\u5982 COUNT\u3001SUM\uff09\u4f7f\u7528\u3002 SELECT column_name, COUNT(*) AS count FROM table_name GROUP BY column_name; \u767b\u5f55\u540e\u590d\u5236 3. UNION ALL \u548c DISTINCT UNION ALL \u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u4e3a\u4e00\u4e2a\u7ed3\u679c\u96c6\u3002\u7ed3\u5408 DISTINCT \u53ef\u4ee5\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u3002 SELECT [&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-5876","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5876","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=5876"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5876\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}