{"id":2344,"date":"2025-03-04T08:45:40","date_gmt":"2025-03-04T00:45:40","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2344"},"modified":"2025-03-04T08:45:40","modified_gmt":"2025-03-04T00:45:40","slug":"sqlite-distinct-%e5%85%b3%e9%94%ae%e5%ad%97","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/04\/sqlite-distinct-%e5%85%b3%e9%94%ae%e5%ad%97\/","title":{"rendered":"SQLite DISTINCT \u5173\u952e\u5b57"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><code>DISTINCT<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u53bb\u9664\u91cd\u590d\u7684\u884c\uff0c\u786e\u4fdd\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u4e2d\u7684\u6bcf\u4e00\u884c\u90fd\u662f\u552f\u4e00\u7684\u3002\u5b83\u901a\u5e38\u4e0e <code>SELECT<\/code> \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u83b7\u53d6\u4e0d\u91cd\u590d\u7684\u8bb0\u5f55\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u57fa\u672c\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DISTINCT column1, column2, ...\nFROM table_name\nWHERE condition;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>DISTINCT<\/code>: \u7528\u4e8e\u53bb\u9664\u91cd\u590d\u7684\u884c\u3002<\/li>\n\n\n\n<li><code>column1, column2, ...<\/code>: \u9700\u8981\u67e5\u8be2\u7684\u5217\u3002<\/li>\n\n\n\n<li><code>table_name<\/code>: \u67e5\u8be2\u7684\u6570\u636e\u8868\u3002<\/li>\n\n\n\n<li><code>condition<\/code>: \u53ef\u9009\u7684\u67e5\u8be2\u6761\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u8fd4\u56de\u552f\u4e00\u7684\u5355\u5217\u503c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DISTINCT<\/code> \u53ef\u4ee5\u7528\u4e8e\u8fd4\u56de\u67d0\u4e00\u5217\u4e2d\u7684\u552f\u4e00\u503c\u3002\u91cd\u590d\u7684\u503c\u4f1a\u88ab\u53bb\u9664\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u83b7\u53d6 <code>employees<\/code> \u8868\u4e2d\u6240\u6709\u4e0d\u540c\u7684 <code>department<\/code> \u503c\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT DISTINCT department\n  FROM employees;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6240\u6709\u4e0d\u540c\u7684\u90e8\u95e8\u540d\u79f0\uff0c\u53bb\u9664\u4e86\u91cd\u590d\u7684\u90e8\u95e8\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u8fd4\u56de\u552f\u4e00\u7684\u591a\u5217\u7ec4\u5408<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DISTINCT<\/code> \u4e0d\u4ec5\u53ef\u4ee5\u5e94\u7528\u4e8e\u5355\u5217\uff0c\u4e5f\u53ef\u4ee5\u5e94\u7528\u4e8e\u591a\u4e2a\u5217\u3002\u5b83\u5c06\u8fd4\u56de\u6240\u6709\u5217\u7ec4\u5408\u7684\u552f\u4e00\u884c\u3002\u5982\u679c\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u7684\u7ec4\u5408\u5b8c\u5168\u76f8\u540c\uff0c\u90a3\u4e48\u53ea\u6709\u4e00\u884c\u4f1a\u88ab\u4fdd\u7559\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u83b7\u53d6 <code>employees<\/code> \u8868\u4e2d\u4e0d\u540c\u7684 <code>department<\/code> \u548c <code>job_title<\/code> \u7ec4\u5408\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT DISTINCT department, job_title\n  FROM employees;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6bcf\u4e2a\u90e8\u95e8\u548c\u804c\u4f4d\u7684\u552f\u4e00\u7ec4\u5408\u3002\u5982\u679c\u6709\u76f8\u540c\u90e8\u95e8\u548c\u804c\u4f4d\u7684\u591a\u6761\u8bb0\u5f55\uff0c\u53ea\u8fd4\u56de\u4e00\u6761\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. <code>DISTINCT<\/code> \u4e0e <code>WHERE<\/code> \u5b50\u53e5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DISTINCT<\/code> \u53ef\u4ee5\u4e0e <code>WHERE<\/code> \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5148\u6839\u636e\u6307\u5b9a\u6761\u4ef6\u7b5b\u9009\u6570\u636e\uff0c\u518d\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u83b7\u53d6\u85aa\u8d44\u5927\u4e8e 3000 \u7684\u4e0d\u540c <code>department<\/code>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT DISTINCT department\n  FROM employees\n  WHERE salary &gt; 3000;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6240\u6709\u85aa\u8d44\u5927\u4e8e 3000 \u7684\u552f\u4e00\u90e8\u95e8\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. <code>DISTINCT<\/code> \u4e0e \u805a\u5408\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4f1a\u60f3\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u805a\u5408\u540e\u518d\u53bb\u91cd\u3002<code>DISTINCT<\/code> \u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u4e0e\u805a\u5408\u51fd\u6570\uff08\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code> \u7b49\uff09\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u95e8\u4e2d\u4e0d\u540c\u804c\u4f4d\u7684\u6570\u91cf\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department, COUNT(DISTINCT job_title)\n  FROM employees\n  GROUP BY department;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6bcf\u4e2a\u90e8\u95e8\u5185\u4e0d\u540c\u804c\u4f4d\u7684\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. <code>DISTINCT<\/code> \u4e0e <code>ORDER BY<\/code> \u6392\u5e8f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DISTINCT<\/code> \u548c <code>ORDER BY<\/code> \u53ef\u4ee5\u4e00\u8d77\u4f7f\u7528\uff0c\u9996\u5148\u53bb\u9664\u91cd\u590d\u7684\u884c\uff0c\u7136\u540e\u6309\u6307\u5b9a\u7684\u5217\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u83b7\u53d6\u6240\u6709\u4e0d\u540c\u7684 <code>department<\/code>\uff0c\u5e76\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT DISTINCT department\n  FROM employees\n  ORDER BY department;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u9996\u5148\u53bb\u9664\u91cd\u590d\u7684\u90e8\u95e8\uff0c\u7136\u540e\u6309\u90e8\u95e8\u540d\u79f0\u5347\u5e8f\u6392\u5217\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>DISTINCT<\/code><\/strong> \u7528\u4e8e\u53bb\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u91cd\u590d\u884c\uff0c\u786e\u4fdd\u6bcf\u4e00\u884c\u90fd\u662f\u552f\u4e00\u7684\u3002<\/li>\n\n\n\n<li>\u5b83\u53ef\u4ee5\u4e0e\u5355\u5217\u6216\u591a\u5217\u4e00\u8d77\u4f7f\u7528\uff0c\u53bb\u9664\u91cd\u590d\u7684\u5217\u503c\u6216\u5217\u7ec4\u5408\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u4e0e <strong><code>WHERE<\/code><\/strong> \u5b50\u53e5\u3001\u805a\u5408\u51fd\u6570\u3001<strong><code>ORDER BY<\/code><\/strong> \u7b49\u4e00\u8d77\u4f7f\u7528\uff0c\u589e\u52a0\u67e5\u8be2\u7684\u7075\u6d3b\u6027\u3002<\/li>\n\n\n\n<li>\u5f53\u4f60\u5e0c\u671b\u5f97\u5230\u4e0d\u91cd\u590d\u7684\u6570\u636e\u65f6\uff0c<code>DISTINCT<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4f7f\u7528 <code>DISTINCT<\/code>\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u6570\u636e\u96c6\u4e2d\u83b7\u53d6\u552f\u4e00\u7684\u503c\u6216\u7ec4\u5408\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u53bb\u9664\u91cd\u590d\u7684\u884c\uff0c\u786e\u4fdd\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u4e2d\u7684\u6bcf\u4e00\u884c\u90fd\u662f\u552f\u4e00\u7684\u3002\u5b83\u901a\u5e38\u4e0e SELE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2344","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2344"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2344\/revisions"}],"predecessor-version":[{"id":2345,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2344\/revisions\/2345"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}