{"id":6092,"date":"2024-11-14T11:31:06","date_gmt":"2024-11-14T03:31:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6092\/"},"modified":"2024-11-14T11:31:06","modified_gmt":"2024-11-14T03:31:06","slug":"oracle%e5%8e%bb%e9%87%8d%e5%a6%82%e4%bd%95%e5%8a%a0%e6%9d%a1%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6092\/","title":{"rendered":"oracle\u53bb\u91cd\u5982\u4f55\u52a0\u6761\u4ef6"},"content":{"rendered":"<blockquote><p>\n  oracle \u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5\u6709\uff1adistinct\uff1a\u79fb\u9664\u6240\u6709\u5217\u503c\u76f8\u540c\u7684\u91cd\u590d\u884c\u3002group by\uff1a\u5c06\u76f8\u540c\u503c\u7684\u884c\u5206\u7ec4\uff0c\u4ec5\u8fd4\u56de\u4e00\u4e2a\u4ee3\u8868\u884c\u3002row_number()\uff1a\u4e3a\u6bcf\u4e00\u884c\u5206\u914d\u552f\u4e00\u6574\u6570\uff0c\u7528\u4e8e\u6807\u8bc6\u5e76\u5220\u9664\u91cd\u590d\u884c\u3002\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u6761\u4ef6\uff0c\u4ec5\u79fb\u9664\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u91cd\u590d\u884c\uff0c\u5982 salary \u503c\u5927\u4e8e 5000\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/12\/2024061222391996021.jpg\" class=\"aligncenter\" title=\"oracle\u53bb\u91cd\u5982\u4f55\u52a0\u6761\u4ef6\u63d2\u56fe\" alt=\"oracle\u53bb\u91cd\u5982\u4f55\u52a0\u6761\u4ef6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5\u53ca\u6dfb\u52a0\u6761\u4ef6<\/strong><\/p>\n<p>Oracle\u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u6cd5\u6765\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u5305\u62ec DISTINCT\u3001GROUP BY \u548c ROW_NUMBER()\u3002<\/p>\n<p><strong>\u4f7f\u7528 DISTINCT<\/strong><\/p>\n<p>DISTINCT \u8fd0\u7b97\u7b26\u53ef\u4ece\u7ed3\u679c\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u7684\u884c\u3002\u5b83\u5c06\u6839\u636e\u6240\u6709\u5217\u7684\u503c\u6765\u5224\u65ad\u91cd\u590d\u6027\u3002<\/p>\n<pre>SELECT DISTINCT column1, column2\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 GROUP BY<\/strong><\/p>\n<p>GROUP BY \u8fd0\u7b97\u7b26\u5c06\u5177\u6709\u76f8\u540c\u503c\u7684\u884c\u5206\u7ec4\uff0c\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u4e00\u4e2a\u4ee3\u8868\u884c\u3002<\/p>\n<pre>SELECT column1, column2\nFROM table_name\nGROUP BY column1, column2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 ROW_NUMBER()<\/strong><\/p>\n<p>ROW_NUMBER() \u51fd\u6570\u4e3a\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684\u6574\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8be5\u503c\u6765\u6807\u8bc6\u91cd\u590d\u7684\u884c\u5e76\u5c06\u5176\u5220\u9664\u3002<\/p>\n<pre>WITH CTE AS (\n    SELECT column1, column2, ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY id) AS row_num\n    FROM table_name\n)\nSELECT column1, column2\nFROM CTE\nWHERE row_num = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6dfb\u52a0\u6761\u4ef6<\/strong><\/p>\n<p>\u53ef\u4ee5\u5728\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u6761\u4ef6\uff0c\u4ee5\u4ec5\u79fb\u9664\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u91cd\u590d\u884c\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u4f7f\u7528 DISTINCT \u6765\u79fb\u9664 salary \u5217\u503c\u5927\u4e8e 5000 \u7684\u91cd\u590d\u884c\uff1a<\/p>\n<pre>SELECT DISTINCT column1, column2\nFROM table_name\nWHERE salary &gt; 5000;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 GROUP BY \u548c HAVING \u5b50\u53e5\u6765\u79fb\u9664\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u91cd\u590d\u7ec4\uff1a<\/p>\n<pre>SELECT column1, column2\nFROM table_name\nGROUP BY column1, column2\nHAVING COUNT(*) &gt; 1 AND SUM(salary) &gt; 10000;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u53bb\u91cd\u5982\u4f55\u52a0\u6761\u4ef6\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>oracle \u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5\u6709\uff1adistinct\uff1a\u79fb\u9664\u6240\u6709\u5217\u503c\u76f8\u540c\u7684\u91cd\u590d\u884c\u3002group by\uff1a\u5c06\u76f8\u540c\u503c\u7684\u884c\u5206\u7ec4\uff0c\u4ec5\u8fd4\u56de\u4e00\u4e2a\u4ee3\u8868\u884c\u3002row_number()\uff1a\u4e3a\u6bcf\u4e00\u884c\u5206\u914d\u552f\u4e00\u6574\u6570\uff0c\u7528\u4e8e\u6807\u8bc6\u5e76\u5220\u9664\u91cd\u590d\u884c\u3002\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u6761\u4ef6\uff0c\u4ec5\u79fb\u9664\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u91cd\u590d\u884c\uff0c\u5982 salary \u503c\u5927\u4e8e 5000\u3002 \u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5\u53ca\u6dfb\u52a0\u6761\u4ef6 Oracle\u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u6cd5\u6765\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u5305\u62ec DISTINCT\u3001GROUP BY \u548c ROW_NUMBER()\u3002 \u4f7f\u7528 DISTINCT DISTINCT \u8fd0\u7b97\u7b26\u53ef\u4ece\u7ed3\u679c\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u7684\u884c\u3002\u5b83\u5c06\u6839\u636e\u6240\u6709\u5217\u7684\u503c\u6765\u5224\u65ad\u91cd\u590d\u6027\u3002 SELECT DISTINCT column1, column2 FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 GROUP BY GROUP BY \u8fd0\u7b97\u7b26\u5c06\u5177\u6709\u76f8\u540c\u503c\u7684\u884c\u5206\u7ec4\uff0c\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u4e00\u4e2a\u4ee3\u8868\u884c\u3002 SELECT column1, column2 FROM table_name GROUP BY column1, column2; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 ROW_NUMBER() ROW_NUMBER() \u51fd\u6570\u4e3a\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684\u6574\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8be5\u503c\u6765\u6807\u8bc6\u91cd\u590d\u7684\u884c\u5e76\u5c06\u5176\u5220\u9664\u3002 WITH CTE AS ( SELECT column1, column2, ROW_NUMBER() OVER (PARTITION BY column1, [&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-6092","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6092","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=6092"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6092\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}