{"id":6908,"date":"2024-11-14T10:46:40","date_gmt":"2024-11-14T02:46:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6908\/"},"modified":"2024-11-14T10:46:40","modified_gmt":"2024-11-14T02:46:40","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mysql-%e6%8c%89%e6%9d%a1%e4%bb%b6%e7%ad%9b%e9%80%89-distinct-%e5%ad%97%e6%ae%b5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6908\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 MySQL \u6309\u6761\u4ef6\u7b5b\u9009 DISTINCT \u5b57\u6bb5\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173007523438897.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 MySQL \u6309\u6761\u4ef6\u7b5b\u9009 DISTINCT \u5b57\u6bb5\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 MySQL \u6309\u6761\u4ef6\u7b5b\u9009 DISTINCT \u5b57\u6bb5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u901a\u8fc7  \u6309\u6761\u4ef6\u7b5b\u9009 distinct \u5b57\u6bb5<\/strong><\/p>\n<p>\u60f3\u8981\u6309\u6761\u4ef6\u7b5b\u9009 distinct \u5b57\u6bb5\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4f7f\u7528 distinct \u548c\u5b50\u67e5\u8be2<\/strong><\/p>\n<p>distinct \u64cd\u4f5c\u7b26\u53ef\u4ee5\u4ece\u7ed3\u679c\u96c6\u4e2d\u53bb\u9664\u91cd\u590d\u503c\u3002\u7ed3\u5408\u5b50\u67e5\u8be2\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6765\u7b5b\u9009\u5b57\u6bb5\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u8981\u6309 domn \u5b57\u6bb5\u53bb\u91cd\uff0c\u4f46\u8981\u8003\u8651 loc \u5b57\u6bb5\u4e2d\u7684 &#8220;\u5883\u5185&#8221; \u548c &#8220;\u5883\u5916&#8221; \u6761\u76ee\uff0c\u53ef\u4ee5\u8fd9\u6837\u7f16\u5199\u67e5\u8be2\uff1a<\/p>\n<pre>select distinct domain\nfrom (\n  select domain, loc\n  from table_name\n  where loc = \"\u5883\u5916\"\n) as t1\nunion all\n(\n  select domain, loc\n  from table_name\n  where loc != \"\u5883\u5916\"\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 case \u8bed\u53e5<\/strong><\/p>\n<p>case \u8bed\u53e5\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u6765\u8bc4\u4f30\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u5229\u7528\u8fd9\u4e2a\u7279\u6027\uff0c\u53ef\u4ee5\u521b\u5efa\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT DISTINCT domain\nFROM table_name\nWHERE CASE\n  WHEN loc = \"\u5883\u5916\" THEN domain\n  ELSE NULL\nEND IS NOT NULL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5177\u6709\u552f\u4e00\u57df\u540d\u7684\u884c\uff0c\u540c\u65f6\u8fd8\u8003\u8651 loc \u5b57\u6bb5\u4e2d\u7684 &#8220;\u5883\u5916&#8221; \u6761\u76ee\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 MySQL \u6309\u6761\u4ef6\u7b5b\u9009 DISTINCT \u5b57\u6bb5\uff1f\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>\u901a\u8fc7 \u6309\u6761\u4ef6\u7b5b\u9009 distinct \u5b57\u6bb5 \u60f3\u8981\u6309\u6761\u4ef6\u7b5b\u9009 distinct \u5b57\u6bb5\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u4f7f\u7528 distinct \u548c\u5b50\u67e5\u8be2 distinct \u64cd\u4f5c\u7b26\u53ef\u4ee5\u4ece\u7ed3\u679c\u96c6\u4e2d\u53bb\u9664\u91cd\u590d\u503c\u3002\u7ed3\u5408\u5b50\u67e5\u8be2\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6765\u7b5b\u9009\u5b57\u6bb5\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u8981\u6309 domn \u5b57\u6bb5\u53bb\u91cd\uff0c\u4f46\u8981\u8003\u8651 loc \u5b57\u6bb5\u4e2d\u7684 &#8220;\u5883\u5185&#8221; \u548c &#8220;\u5883\u5916&#8221; \u6761\u76ee\uff0c\u53ef\u4ee5\u8fd9\u6837\u7f16\u5199\u67e5\u8be2\uff1a select distinct domain from ( select domain, loc from table_name where loc = &#8220;\u5883\u5916&#8221; ) as t1 union all ( select domain, loc from table_name where loc != &#8220;\u5883\u5916&#8221; ); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 case \u8bed\u53e5 [&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-6908","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6908","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=6908"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6908\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}