{"id":5773,"date":"2024-11-14T10:55:15","date_gmt":"2024-11-14T02:55:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5773\/"},"modified":"2024-11-14T10:55:15","modified_gmt":"2024-11-14T02:55:15","slug":"mysql%e5%8e%bb%e9%87%8d%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5773\/","title":{"rendered":"mysql\u53bb\u91cd\u662f\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<blockquote><p>\n  mysql \u53bb\u91cd\u662f\u6307\u4ece\u6570\u636e\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u503c\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u5e38\u89c1\u7684\u53bb\u91cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u79fb\u9664\u91cd\u590d\u503c\u4f7f\u7528 group by \u5b50\u53e5\u5bf9\u6570\u636e\u5206\u7ec4\uff0c\u5e76\u4fdd\u7559\u6bcf\u7ec4\u4e2d\u7684\u552f\u4e00\u503c\u4f7f\u7528 unique \u7ea6\u675f\u6216\u7d22\u5f15\u5f3a\u5236\u6570\u636e\u5e93\u4fdd\u6301\u552f\u4e00\u503c\u4f7f\u7528 union all \u64cd\u4f5c\u7b26\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u5e76\u53bb\u9664\u91cd\u590d\u503c\u4f7f\u7528\u4e34\u65f6\u8868\u5b58\u50a8\u4e2d\u95f4\u7ed3\u679c\u5e76\u4f7f\u7528 not in \u5b50\u53e5\u8fc7\u6ee4\u91cd\u590d\u503c\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/02\/2024060201092254983.jpg\" class=\"aligncenter\" title=\"mysql\u53bb\u91cd\u662f\u4ec0\u4e48\u610f\u601d\u63d2\u56fe\" alt=\"mysql\u53bb\u91cd\u662f\u4ec0\u4e48\u610f\u601d\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u53bb\u91cd<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f MySQL \u53bb\u91cd\uff1f<\/strong><\/p>\n<p>MySQL \u53bb\u91cd\u662f\u6307\u4ece\u6570\u636e\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u503c\uff0c\u4ec5\u4fdd\u7559\u552f\u4e00\u503c\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5b9e\u73b0 MySQL \u53bb\u91cd\uff1f<\/strong><\/p>\n<p>\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0 MySQL \u53bb\u91cd\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u6216 GROUP BY \u5b50\u53e5\u3002<\/p>\n<p><strong>\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u79fb\u9664\u91cd\u590d\u7684\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT DISTINCT column_name(s)\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>\u8fd9\u5c06\u4ece students \u8868\u4e2d\u9009\u62e9 name \u5217\u7684\u552f\u4e00\u503c\u3002<\/p>\n<p><strong>\u4f7f\u7528 GROUP BY \u5b50\u53e5<\/strong><\/p>\n<p>GROUP BY \u5b50\u53e5\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u4ec5\u4fdd\u7559\u6bcf\u7ec4\u4e2d\u7684\u552f\u4e00\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT column_name(s)\nFROM table_name\nGROUP BY column_name(s);<\/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>\u8fd9\u5c06\u4ece students \u8868\u4e2d\u9009\u62e9 name \u5217\u7684\u552f\u4e00\u503c\uff0c\u5e76\u6309 name \u8fdb\u884c\u5206\u7ec4\u3002<\/p>\n<p><strong>\u5176\u4ed6\u53bb\u91cd\u6280\u5de7<\/strong><\/p>\n<p>\u9664\u4e86 DISTINCT \u548c GROUP BY \u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6280\u5de7\u5b9e\u73b0\u53bb\u91cd\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 UNIQUE \u7ea6\u675f\u6216\u7d22\u5f15\u6765\u5f3a\u5236\u6570\u636e\u5e93\u4fdd\u6301\u552f\u4e00\u503c\u3002<\/li>\n<li>\u4f7f\u7528 UNION ALL \u64cd\u4f5c\u7b26\u5c06\u591a\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u5408\u5e76\u5728\u4e00\u8d77\uff0c\u5e76\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u503c\u3002<\/li>\n<li>\u4f7f\u7528\u4e34\u65f6\u8868\u5b58\u50a8\u4e2d\u95f4\u7ed3\u679c\uff0c\u5e76\u4f7f\u7528 NOT IN \u5b50\u53e5\u8fc7\u6ee4\u91cd\u590d\u503c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u53bb\u91cd\u662f\u4ec0\u4e48\u610f\u601d\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 \u53bb\u91cd\u662f\u6307\u4ece\u6570\u636e\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u503c\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u5e38\u89c1\u7684\u53bb\u91cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u79fb\u9664\u91cd\u590d\u503c\u4f7f\u7528 group by \u5b50\u53e5\u5bf9\u6570\u636e\u5206\u7ec4\uff0c\u5e76\u4fdd\u7559\u6bcf\u7ec4\u4e2d\u7684\u552f\u4e00\u503c\u4f7f\u7528 unique \u7ea6\u675f\u6216\u7d22\u5f15\u5f3a\u5236\u6570\u636e\u5e93\u4fdd\u6301\u552f\u4e00\u503c\u4f7f\u7528 union all \u64cd\u4f5c\u7b26\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u5e76\u53bb\u9664\u91cd\u590d\u503c\u4f7f\u7528\u4e34\u65f6\u8868\u5b58\u50a8\u4e2d\u95f4\u7ed3\u679c\u5e76\u4f7f\u7528 not in \u5b50\u53e5\u8fc7\u6ee4\u91cd\u590d\u503c MySQL \u53bb\u91cd \u4ec0\u4e48\u662f MySQL \u53bb\u91cd\uff1f MySQL \u53bb\u91cd\u662f\u6307\u4ece\u6570\u636e\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u503c\uff0c\u4ec5\u4fdd\u7559\u552f\u4e00\u503c\u7684\u8fc7\u7a0b\u3002 \u5982\u4f55\u5b9e\u73b0 MySQL \u53bb\u91cd\uff1f \u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0 MySQL \u53bb\u91cd\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u6216 GROUP BY \u5b50\u53e5\u3002 \u4f7f\u7528 DISTINCT \u5173\u952e\u5b57 DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u79fb\u9664\u91cd\u590d\u7684\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT DISTINCT column_name(s) FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff1a SELECT DISTINCT name FROM students; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u4ece students \u8868\u4e2d\u9009\u62e9 name [&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-5773","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5773","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=5773"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5773\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}