{"id":5436,"date":"2024-11-14T13:21:56","date_gmt":"2024-11-14T05:21:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5436\/"},"modified":"2024-11-14T13:21:56","modified_gmt":"2024-11-14T05:21:56","slug":"mysql%e5%8e%bb%e9%87%8d%e6%80%8e%e4%b9%88%e5%8e%bb","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5436\/","title":{"rendered":"mysql\u53bb\u91cd\u600e\u4e48\u53bb"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e2d\u7684\u6570\u636e\u53bb\u91cd\u65b9\u6cd5\u6709\uff1adistinct \u5173\u952e\u5b57\uff1a\u6d88\u9664\u91cd\u590d\u884c\uff0c\u4ec5\u4fdd\u7559\u4e0d\u91cd\u590d\u6570\u636e\u3002group by \u8bed\u53e5\uff1a\u5206\u7ec4\u540e\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\u3002unique \u7d22\u5f15\uff1a\u4e3a\u7279\u5b9a\u5217\u521b\u5efa\u552f\u4e00\u7d22\u5f15\uff0c\u786e\u4fdd\u8be5\u5217\u4e2d\u7684\u6570\u636e\u552f\u4e00\u3002union all \u64cd\u4f5c\u7b26\uff1a\u5408\u5e76\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\uff0c\u540c\u65f6\u4fdd\u7559\u91cd\u590d\u884c\uff0c\u518d\u901a\u8fc7\u81ea\u8fde\u63a5\u53bb\u9664\u91cd\u590d\u884c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052813092524523.jpg\" class=\"aligncenter\" title=\"mysql\u53bb\u91cd\u600e\u4e48\u53bb\u63d2\u56fe\" alt=\"mysql\u53bb\u91cd\u600e\u4e48\u53bb\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d\u5982\u4f55\u53bb\u91cd<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\u8fdb\u884c\u6570\u636e\u53bb\u91cd\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002<\/p>\n<p><strong>1. \u4f7f\u7528 DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u6d88\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u91cd\u590d\u884c\uff0c\u4ec5\u4fdd\u7559\u4e0d\u91cd\u590d\u7684\u6570\u636e\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT DISTINCT column_name\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u53bb\u9664\u8868 employees \u4e2d name \u5217\u4e2d\u7684\u91cd\u590d\u503c\uff1a<\/p>\n<pre>SELECT DISTINCT name\nFROM employees;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 GROUP BY \u8bed\u53e5<\/strong><\/p>\n<p>GROUP BY \u8bed\u53e5\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\uff0c\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/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\uff0c\u53bb\u9664\u8868 orders \u4e2d product_id \u5217\u4e2d\u7684\u91cd\u590d\u503c\uff1a<\/p>\n<pre>SELECT product_id\nFROM orders\nGROUP BY product_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 UNIQUE \u7d22\u5f15<\/strong><\/p>\n<p>\u4e3a\u8868\u4e2d\u7684\u7279\u5b9a\u5217\u521b\u5efa\u552f\u4e00\u7d22\u5f15\uff0c\u53ef\u4ee5\u786e\u4fdd\u8be5\u5217\u4e2d\u7684\u6570\u636e\u552f\u4e00\u3002\u521b\u5efa\u552f\u4e00\u7d22\u5f15\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CREATE UNIQUE INDEX index_name ON table_name(column_name);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u4e3a\u8868 customers \u4e2d\u7684 email \u5217\u521b\u5efa\u552f\u4e00\u7d22\u5f15\uff0c\u4ee5\u9632\u6b62\u91cd\u590d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a<\/p>\n<pre>CREATE UNIQUE INDEX email_index ON customers(email);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 UNION ALL<\/strong><\/p>\n<p>UNION ALL \u64cd\u4f5c\u7b26\u53ef\u4ee5\u5c06\u591a\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u5408\u5e76\u4e3a\u4e00\u4e2a\u7ed3\u679c\u96c6\uff0c\u540c\u65f6\u4fdd\u7559\u6240\u6709\u884c\uff0c\u5305\u62ec\u91cd\u590d\u7684\u884c\u3002\u901a\u8fc7\u5c06\u67e5\u8be2\u4e0e UNION ALL \u81ea\u8eab\u8fdb\u884c\u8fde\u63a5\uff0c\u53ef\u4ee5\u53bb\u9664\u91cd\u590d\u884c\u3002<\/p>\n<pre>SELECT column_name\nFROM table_name\nUNION ALL\nSELECT column_name\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u53bb\u9664\u8868 products \u4e2d name \u5217\u4e2d\u7684\u91cd\u590d\u503c\uff1a<\/p>\n<pre>SELECT name\nFROM products\nUNION ALL\nSELECT name\nFROM products;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u53bb\u91cd\u600e\u4e48\u53bb\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>mysql \u4e2d\u7684\u6570\u636e\u53bb\u91cd\u65b9\u6cd5\u6709\uff1adistinct \u5173\u952e\u5b57\uff1a\u6d88\u9664\u91cd\u590d\u884c\uff0c\u4ec5\u4fdd\u7559\u4e0d\u91cd\u590d\u6570\u636e\u3002group by \u8bed\u53e5\uff1a\u5206\u7ec4\u540e\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\u3002unique \u7d22\u5f15\uff1a\u4e3a\u7279\u5b9a\u5217\u521b\u5efa\u552f\u4e00\u7d22\u5f15\uff0c\u786e\u4fdd\u8be5\u5217\u4e2d\u7684\u6570\u636e\u552f\u4e00\u3002union all \u64cd\u4f5c\u7b26\uff1a\u5408\u5e76\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\uff0c\u540c\u65f6\u4fdd\u7559\u91cd\u590d\u884c\uff0c\u518d\u901a\u8fc7\u81ea\u8fde\u63a5\u53bb\u9664\u91cd\u590d\u884c\u3002 MySQL \u4e2d\u5982\u4f55\u53bb\u91cd \u5728 MySQL \u4e2d\u8fdb\u884c\u6570\u636e\u53bb\u91cd\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002 1. \u4f7f\u7528 DISTINCT \u5173\u952e\u5b57 DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u6d88\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u91cd\u590d\u884c\uff0c\u4ec5\u4fdd\u7559\u4e0d\u91cd\u590d\u7684\u6570\u636e\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a SELECT DISTINCT column_name FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u53bb\u9664\u8868 employees \u4e2d name \u5217\u4e2d\u7684\u91cd\u590d\u503c\uff1a SELECT DISTINCT name FROM employees; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 GROUP BY \u8bed\u53e5 GROUP BY \u8bed\u53e5\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\uff0c\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a SELECT column_name FROM table_name GROUP BY column_name; \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u53bb\u9664\u8868 orders \u4e2d [&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-5436","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5436","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=5436"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5436\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}