{"id":5610,"date":"2024-11-14T12:39:15","date_gmt":"2024-11-14T04:39:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5610\/"},"modified":"2024-11-14T12:39:15","modified_gmt":"2024-11-14T04:39:15","slug":"sql%e8%af%ad%e5%8f%a5%e5%8e%bb%e9%87%8d%e6%9f%a5%e8%af%a2%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5610\/","title":{"rendered":"sql\u8bed\u53e5\u53bb\u91cd\u67e5\u8be2\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  sql \u53bb\u91cd\u67e5\u8be2\u4f7f\u7528 distinct \u5173\u952e\u5b57\u548c select \u8bed\u53e5\u4ece\u7ed3\u679c\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u8bb0\u5f55\u3002\u5b83\u652f\u6301\u4f7f\u7528 where \u5b50\u53e5\u548c group by \u5b50\u53e5\u8fdb\u884c\u8fc7\u6ee4\u548c\u5206\u7ec4\u3002\u793a\u4f8b\uff1aselect distinct column_name(s) from table_name where &#8230; group by column_name(s)\uff1b\u8fd9\u5c06\u8fd4\u56de\u53ea\u5305\u542b\u552f\u4e00\u503c\u7684\u5217\uff0c\u5e76\u53ef\u4f7f\u7528 where \u548c group by \u5b50\u53e5\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u8fc7\u6ee4\u548c\u5206\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053018543850189.jpg\" class=\"aligncenter\" title=\"sql\u8bed\u53e5\u53bb\u91cd\u67e5\u8be2\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"sql\u8bed\u53e5\u53bb\u91cd\u67e5\u8be2\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u53bb\u91cd\u67e5\u8be2<\/strong><\/p>\n<p>SQL \u4e2d\u53bb\u91cd\u67e5\u8be2\u7528\u4e8e\u4ece\u7ed3\u679c\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u53ea\u4fdd\u7559\u552f\u4e00\u503c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT DISTINCT column_name(s)\nFROM table_name\nWHERE ...\nGROUP BY column_name(s)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u53bb\u91cd\u3002<\/li>\n<li>column_name(s) \u6307\u5b9a\u8981\u8fdb\u884c\u53bb\u91cd\u7684\u5217\u3002<\/li>\n<li>WHERE \u5b50\u53e5\u53ef\u7528\u4e8e\u8fc7\u6ee4\u7ed3\u679c\u96c6\u3002<\/li>\n<li>GROUP BY \u5b50\u53e5\u53ef\u7528\u4e8e\u5bf9\u7ed3\u679c\u4ee5\u6307\u5b9a\u5217\u8fdb\u884c\u5206\u7ec4\uff08\u53ef\u9009\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a students \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n<pre>| student_id | student_name |\n|---|---|\n| 1 | John Doe |\n| 2 | Jane Smith |\n| 3 | John Doe |\n| 4 | Mary Jones |<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8981\u4ece\u8868\u4e2d\u53bb\u9664\u91cd\u590d\u7684 student_name \u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT DISTINCT student_name\nFROM students;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre>| student_name |\n|---|---|\n| John Doe |\n| Jane Smith |\n| Mary Jones |<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 GROUP BY \u8fdb\u884c\u5206\u7ec4\uff1a<\/strong><\/p>\n<p>GROUP BY \u5b50\u53e5\u53ef\u7528\u4e8e\u5c06\u7ed3\u679c\u4ee5\u6307\u5b9a\u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u53ea\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u552f\u4e00\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT DISTINCT student_name\nFROM students\nGROUP BY student_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre>| student_name |\n|---|---|\n| John Doe |\n| Jane Smith |\n| Mary Jones |<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u53bb\u91cd\u67e5\u8be2\u600e\u4e48\u5199\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 \u53bb\u91cd\u67e5\u8be2\u4f7f\u7528 distinct \u5173\u952e\u5b57\u548c select \u8bed\u53e5\u4ece\u7ed3\u679c\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u8bb0\u5f55\u3002\u5b83\u652f\u6301\u4f7f\u7528 where \u5b50\u53e5\u548c group by \u5b50\u53e5\u8fdb\u884c\u8fc7\u6ee4\u548c\u5206\u7ec4\u3002\u793a\u4f8b\uff1aselect distinct column_name(s) from table_name where &#8230; group by column_name(s)\uff1b\u8fd9\u5c06\u8fd4\u56de\u53ea\u5305\u542b\u552f\u4e00\u503c\u7684\u5217\uff0c\u5e76\u53ef\u4f7f\u7528 where \u548c group by \u5b50\u53e5\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u8fc7\u6ee4\u548c\u5206\u7ec4\u3002 SQL \u53bb\u91cd\u67e5\u8be2 SQL \u4e2d\u53bb\u91cd\u67e5\u8be2\u7528\u4e8e\u4ece\u7ed3\u679c\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u53ea\u4fdd\u7559\u552f\u4e00\u503c\u3002 \u8bed\u6cd5\uff1a SELECT DISTINCT column_name(s) FROM table_name WHERE &#8230; GROUP BY column_name(s) \u767b\u5f55\u540e\u590d\u5236 \u8bf4\u660e\uff1a DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u53bb\u91cd\u3002 column_name(s) \u6307\u5b9a\u8981\u8fdb\u884c\u53bb\u91cd\u7684\u5217\u3002 WHERE \u5b50\u53e5\u53ef\u7528\u4e8e\u8fc7\u6ee4\u7ed3\u679c\u96c6\u3002 GROUP BY \u5b50\u53e5\u53ef\u7528\u4e8e\u5bf9\u7ed3\u679c\u4ee5\u6307\u5b9a\u5217\u8fdb\u884c\u5206\u7ec4\uff08\u53ef\u9009\uff09\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a students \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u6570\u636e\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-5610","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5610","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=5610"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5610\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}