{"id":5925,"date":"2024-11-14T09:14:37","date_gmt":"2024-11-14T01:14:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5925\/"},"modified":"2024-11-14T09:14:37","modified_gmt":"2024-11-14T01:14:37","slug":"sql%e6%80%8e%e4%b9%88%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5925\/","title":{"rendered":"sql\u600e\u4e48\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u53bb\u91cd\u6709\u56db\u79cd\u65b9\u6cd5\uff1a1. distinct \u5173\u952e\u5b57\uff1a\u6d88\u9664\u5355\u4e00\u5217\u91cd\u590d\u503c\u30022. group by \u5b50\u53e5\uff1a\u6309\u5217\u5206\u7ec4\u5e76\u8fd4\u56de\u552f\u4e00\u884c\u30023. \u5b50\u67e5\u8be2\uff1a\u4ece\u4e3b\u67e5\u8be2\u9009\u62e9\u552f\u4e00\u503c\u30024. \u96c6\u5408\u8fd0\u7b97\u7b26\uff1a\u5408\u5e76\u7ed3\u679c\u96c6\u5e76\u6d88\u9664\u91cd\u590d\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060322334250871.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<h2>SQL \u4e2d\u7684\u53bb\u91cd\u65b9\u6cd5<\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528 SQL \u53bb\u91cd\uff1f<\/strong><\/p>\n<p>SQL \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u53bb\u91cd\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>1. DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u53ef\u4ece\u7ed3\u679c\u96c6\u4e2d\u6d88\u9664\u91cd\u590d\u503c\u3002<\/li>\n<li>\u8bed\u6cd5\uff1aSELECT DISTINCT column_name(s) FROM table_name;<\/li>\n<\/ul>\n<p><strong>2. GROUP BY \u5b50\u53e5<\/strong><\/p>\n<ul>\n<li>GROUP BY \u5b50\u53e5\u53ef\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\u5e76\u53ea\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u552f\u4e00\u4e00\u884c\u3002<\/li>\n<li>\u8bed\u6cd5\uff1aSELECT column_name(s) FROM table_name GROUP BY column_name(s);<\/li>\n<\/ul>\n<p><strong>3. \u5b50\u67e5\u8be2<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5b50\u67e5\u8be2\u53ef\u4ece\u4e3b\u67e5\u8be2\u4e2d\u9009\u62e9\u552f\u4e00\u503c\u3002<\/li>\n<li>\u8bed\u6cd5\uff1aSELECT column_name(s) FROM table_name WHERE column_name IN (SELECT DISTINCT column_name FROM table_name);<\/li>\n<\/ul>\n<p><strong>4. \u96c6\u5408\u8fd0\u7b97\u7b26<\/strong><\/p>\n<ul>\n<li>\u96c6\u5408\u8fd0\u7b97\u7b26UNION\u53ef\u5408\u5e76\u4e24\u4e2a\u7ed3\u679c\u96c6\u5408\u5e76\u6d88\u9664\u91cd\u590d\u503c\u3002<\/li>\n<li>\u8bed\u6cd5\uff1aSELECT column_name(s) FROM table_name UNION SELECT column_name(s) FROM table_name2;<\/li>\n<\/ul>\n<p><strong>\u8be6\u7ec6\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><strong>DISTINCT \u5173\u952e\u5b57\uff1a<\/strong><\/p>\n<pre>SELECT DISTINCT name FROM customers;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>GROUP BY \u5b50\u53e5\uff1a<\/strong><\/p>\n<pre>SELECT name FROM customers GROUP BY name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5b50\u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>SELECT name FROM customers WHERE name IN (SELECT DISTINCT name FROM customers);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u96c6\u5408\u8fd0\u7b97\u7b26\uff1a<\/strong><\/p>\n<pre>SELECT name FROM customers UNION SELECT name FROM orders;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u9009\u62e9\u6700\u4f73\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u53ea\u9700\u8981\u4ece\u5355\u4e00\u5217\u4e2d\u6d88\u9664\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u6839\u636e\u591a\u4e2a\u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 GROUP BY \u5b50\u53e5\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u4ece\u5b50\u67e5\u8be2\u4e2d\u9009\u62e9\u552f\u4e00\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b50\u67e5\u8be2\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u7ed3\u679c\u96c6\u5408\u5e76\u6d88\u9664\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u7b26\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\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>sql \u4e2d\u53bb\u91cd\u6709\u56db\u79cd\u65b9\u6cd5\uff1a1. distinct \u5173\u952e\u5b57\uff1a\u6d88\u9664\u5355\u4e00\u5217\u91cd\u590d\u503c\u30022. group by \u5b50\u53e5\uff1a\u6309\u5217\u5206\u7ec4\u5e76\u8fd4\u56de\u552f\u4e00\u884c\u30023. \u5b50\u67e5\u8be2\uff1a\u4ece\u4e3b\u67e5\u8be2\u9009\u62e9\u552f\u4e00\u503c\u30024. \u96c6\u5408\u8fd0\u7b97\u7b26\uff1a\u5408\u5e76\u7ed3\u679c\u96c6\u5e76\u6d88\u9664\u91cd\u590d\u503c\u3002 SQL \u4e2d\u7684\u53bb\u91cd\u65b9\u6cd5 \u5982\u4f55\u4f7f\u7528 SQL \u53bb\u91cd\uff1f SQL \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u53bb\u91cd\uff0c\u5305\u62ec\uff1a 1. DISTINCT \u5173\u952e\u5b57 \u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u53ef\u4ece\u7ed3\u679c\u96c6\u4e2d\u6d88\u9664\u91cd\u590d\u503c\u3002 \u8bed\u6cd5\uff1aSELECT DISTINCT column_name(s) FROM table_name; 2. GROUP BY \u5b50\u53e5 GROUP BY \u5b50\u53e5\u53ef\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\u5e76\u53ea\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u552f\u4e00\u4e00\u884c\u3002 \u8bed\u6cd5\uff1aSELECT column_name(s) FROM table_name GROUP BY column_name(s); 3. \u5b50\u67e5\u8be2 \u4f7f\u7528\u5b50\u67e5\u8be2\u53ef\u4ece\u4e3b\u67e5\u8be2\u4e2d\u9009\u62e9\u552f\u4e00\u503c\u3002 \u8bed\u6cd5\uff1aSELECT column_name(s) FROM table_name WHERE column_name IN (SELECT DISTINCT column_name FROM [&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-5925","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5925","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=5925"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5925\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}