{"id":6174,"date":"2024-11-14T12:48:19","date_gmt":"2024-11-14T04:48:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6174\/"},"modified":"2024-11-14T12:48:19","modified_gmt":"2024-11-14T04:48:19","slug":"mysql%e5%a6%82%e4%bd%95%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6174\/","title":{"rendered":"mysql\u5982\u4f55\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u4f7f\u7528 mysql \u53bb\u91cd\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u5220\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u91cd\u590d\u884c\u3002\u4f7f\u7528 group by \u5b50\u53e5\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u67e5\u8be2\u7ed3\u679c\u5206\u7ec4\uff0c\u5e76\u8fd4\u56de\u4e00\u7ec4\u4e2d\u7684\u4e00\u884c\u3002\u4f7f\u7528\u8054\u5408\u67e5\u8be2\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5220\u9664\u91cd\u590d\u884c\u3002\u4f7f\u7528 cte \u521b\u5efa\u4e34\u65f6\u8868\uff0c\u5b58\u50a8\u67e5\u8be2\u7ed3\u679c\u5e76\u5220\u9664\u91cd\u590d\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/14\/2024061404212530386.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u53bb\u91cd\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u53bb\u91cd<\/strong><\/p>\n<p>\u53bb\u91cd\u662f\u5220\u9664\u91cd\u590d\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u5728 MySQL \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>DISTINCT \u5173\u952e\u5b57\u53ef\u7528\u4e8e\u5220\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u91cd\u590d\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT DISTINCT column_name\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de column_name \u5217\u4e2d\u552f\u4e00\u503c\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u4f7f\u7528 GROUP BY \u5b50\u53e5<\/strong><\/p>\n<p>GROUP BY \u5b50\u53e5\u53ef\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4ec5\u8fd4\u56de\u4e00\u7ec4\u4e2d\u7684\u4e00\u884c\u3002\u4f8b\u5982\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>\u8fd9\u5c06\u8fd4\u56de column_name \u5217\u4e2d\u6bcf\u4e2a\u552f\u4e00\u503c\u7684\u4e00\u884c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u8054\u5408\u67e5\u8be2<\/strong><\/p>\n<p>\u8054\u5408\u67e5\u8be2\u5141\u8bb8\u60a8\u4ece\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u4e2d\u521b\u5efa\u5355\u4e2a\u7ed3\u679c\u96c6\u3002\u53ef\u4ee5\u4f7f\u7528 UNION \u6216 UNION ALL \u64cd\u4f5c\u7b26\u6765\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5220\u9664\u91cd\u590d\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT column_name\nFROM table_name1\nUNION\nSELECT column_name\nFROM table_name2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de column_name \u5217\u4e2d\u552f\u4e00\u503c\u7684\u5217\u8868\uff0c\u65e0\u8bba\u5b83\u4eec\u662f\u6765\u81ea table_name1 \u8fd8\u662f table_name2\u3002<\/p>\n<p><strong>\u4f7f\u7528 CTE\uff08\u516c\u5171\u8868\u8868\u8fbe\u5f0f\uff09<\/strong><\/p>\n<p>CTE \u53ef\u4ee5\u521b\u5efa\u4e34\u65f6\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u67e5\u8be2\u7ed3\u679c\u3002\u53ef\u4ee5\u4f7f\u7528 CTE \u6765\u5220\u9664\u91cd\u590d\u6570\u636e\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>WITH UniqueData AS (\n  SELECT DISTINCT column_name\n  FROM table_name\n)\nSELECT *\nFROM UniqueData;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a UniqueData \u7684\u4e34\u65f6\u8868\uff0c\u5176\u4e2d\u5305\u542b column_name \u5217\u7684\u552f\u4e00\u503c\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4ece UniqueData \u8868\u4e2d\u9009\u62e9\u6240\u6709\u884c\u3002<\/p>\n<p><strong>\u6027\u80fd\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u53bb\u91cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u91cf\u548c\u67e5\u8be2\u590d\u6742\u5ea6\u3002\u5bf9\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u96c6\uff0cDISTINCT \u53ef\u80fd\u662f\u4e00\u79cd\u5feb\u901f\u4e14\u7b80\u5355\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u8f83\u5927\u7684\u6570\u636e\u96c6\uff0cGROUP BY \u6216 CTE \u53ef\u80fd\u4f1a\u66f4\u6709\u6548\u7387\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u53bb\u91cd\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>\u5982\u4f55\u4f7f\u7528 mysql \u53bb\u91cd\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u5220\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u91cd\u590d\u884c\u3002\u4f7f\u7528 group by \u5b50\u53e5\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u67e5\u8be2\u7ed3\u679c\u5206\u7ec4\uff0c\u5e76\u8fd4\u56de\u4e00\u7ec4\u4e2d\u7684\u4e00\u884c\u3002\u4f7f\u7528\u8054\u5408\u67e5\u8be2\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5220\u9664\u91cd\u590d\u884c\u3002\u4f7f\u7528 cte \u521b\u5efa\u4e34\u65f6\u8868\uff0c\u5b58\u50a8\u67e5\u8be2\u7ed3\u679c\u5e76\u5220\u9664\u91cd\u590d\u6570\u636e\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u53bb\u91cd \u53bb\u91cd\u662f\u5220\u9664\u91cd\u590d\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u5728 MySQL \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002 \u4f7f\u7528 DISTINCT \u5173\u952e\u5b57 DISTINCT \u5173\u952e\u5b57\u53ef\u7528\u4e8e\u5220\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u91cd\u590d\u884c\u3002\u4f8b\u5982\uff1a SELECT DISTINCT column_name FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u8fd4\u56de column_name \u5217\u4e2d\u552f\u4e00\u503c\u7684\u5217\u8868\u3002 \u4f7f\u7528 GROUP BY \u5b50\u53e5 GROUP BY \u5b50\u53e5\u53ef\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4ec5\u8fd4\u56de\u4e00\u7ec4\u4e2d\u7684\u4e00\u884c\u3002\u4f8b\u5982\uff1a SELECT column_name FROM table_name GROUP BY column_name; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u8fd4\u56de column_name \u5217\u4e2d\u6bcf\u4e2a\u552f\u4e00\u503c\u7684\u4e00\u884c\u3002 \u4f7f\u7528\u8054\u5408\u67e5\u8be2 \u8054\u5408\u67e5\u8be2\u5141\u8bb8\u60a8\u4ece\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u4e2d\u521b\u5efa\u5355\u4e2a\u7ed3\u679c\u96c6\u3002\u53ef\u4ee5\u4f7f\u7528 UNION \u6216 UNION ALL \u64cd\u4f5c\u7b26\u6765\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5220\u9664\u91cd\u590d\u884c\u3002\u4f8b\u5982\uff1a [&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-6174","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6174","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=6174"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6174\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}