{"id":7139,"date":"2024-11-14T14:21:50","date_gmt":"2024-11-14T06:21:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7139\/"},"modified":"2024-11-14T14:21:50","modified_gmt":"2024-11-14T06:21:50","slug":"mysql-%e5%ad%90%e6%9f%a5%e8%af%a2%e4%b8%ad-any_value-%e5%92%8c-where-in-%e5%a4%b1%e6%95%88%e7%9a%84%e5%8e%9f%e5%9b%a0%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7139\/","title":{"rendered":"MySQL \u5b50\u67e5\u8be2\u4e2d any_value \u548c WHERE IN \u5931\u6548\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173050977779454.jpg\" class=\"aligncenter\" title=\"MySQL \u5b50\u67e5\u8be2\u4e2d any_value \u548c WHERE IN \u5931\u6548\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"MySQL \u5b50\u67e5\u8be2\u4e2d any_value \u548c WHERE IN \u5931\u6548\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5b50\u67e5\u8be2\u4e2d\u7684any_value\u4e0ewhere in\u5931\u6548<\/strong><\/p>\n<p>\u95ee\u9898\u63cf\u8ff0\uff1a<\/p>\n<p>\u5728mysql\u4e2d\uff0c\u6267\u884c\u5982\u4e0b\uff1a<\/p>\n<pre>select * from test where id in (select any_value(id) from test group by type);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u867d\u7136\u9884\u671f\u8f93\u51fa\u6bcf\u4e2a\u5206\u7ec4\u7684\u7b2c\u4e00\u884c\u8bb0\u5f55\uff0c\u4f46\u7ed3\u679c\u5374\u8fd4\u56de\u4e86\u6574\u4e2a\u8868\u3002<\/p>\n<p>\u95ee\u9898\u5206\u6790\uff1a<\/p>\n<p><strong>\u95ee\u9898\u4e00\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528any_value\u51fd\u6570\u7684\u5b50\u67e5\u8be2\u4f1a\u5bfc\u81f4where in\u5931\u6548\u3002\u56e0\u4e3aany_value\u51fd\u6570\u8fd4\u56de\u4efb\u610f\u4e00\u884c\u5339\u914d\u8bb0\u5f55\u7684\u503c\uff0c\u5b83\u4e0d\u4fdd\u8bc1\u8fd4\u56de\u552f\u4e00\u503c\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4where in\u6bd4\u8f83\u5931\u8d25\u3002<\/p>\n<p><strong>\u95ee\u9898\u4e8c\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u5728\u5b50\u67e5\u8be2\u4e2d\u7701\u7565as id\u522b\u540d\uff0cmysql\u5c06\u65e0\u6cd5\u627e\u5230select id from &#8230;\u8bed\u53e5\u4e2d\u7684id\u5217\uff0c\u56e0\u4e3a\u5b50\u67e5\u8be2\u6ca1\u6709\u547d\u540d\u5176\u7ed3\u679c\u5217\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cmysql\u4f1a\u5c1d\u8bd5\u81ea\u52a8\u751f\u6210\u5217\u540d\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u786e\u5b9a\u7684\u884c\u4e3a\uff0c\u4f8b\u5982\u8fd4\u56de\u6574\u4e2a\u8868\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p><strong>\u95ee\u9898\u4e00\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528group by\u548cmin\u51fd\u6570\u8fd4\u56de\u6bcf\u4e2a\u5206\u7ec4\u7684\u6700\u5c0fid\u503c\uff0c\u4ece\u800c\u786e\u4fdd\u8fd4\u56de\u552f\u4e00\u503c\uff1a<\/p>\n<pre>select * from test where id in (select min(id) from test group by type);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u95ee\u9898\u4e8c\uff1a<\/strong><\/p>\n<p>\u663e\u5f0f\u6307\u5b9a\u5b50\u67e5\u8be2\u7ed3\u679c\u5217\u7684\u522b\u540d\uff1a<\/p>\n<pre>select * from test where id in (select id from (select any_value(id) as id from test GROUP BY type) as temp);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u5b50\u67e5\u8be2\u4e2d any_value \u548c WHERE IN \u5931\u6548\u7684\u539f\u56e0\u662f\u4ec0\u4e48\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>\u5b50\u67e5\u8be2\u4e2d\u7684any_value\u4e0ewhere in\u5931\u6548 \u95ee\u9898\u63cf\u8ff0\uff1a \u5728mysql\u4e2d\uff0c\u6267\u884c\u5982\u4e0b\uff1a select * from test where id in (select any_value(id) from test group by type); \u767b\u5f55\u540e\u590d\u5236 \u867d\u7136\u9884\u671f\u8f93\u51fa\u6bcf\u4e2a\u5206\u7ec4\u7684\u7b2c\u4e00\u884c\u8bb0\u5f55\uff0c\u4f46\u7ed3\u679c\u5374\u8fd4\u56de\u4e86\u6574\u4e2a\u8868\u3002 \u95ee\u9898\u5206\u6790\uff1a \u95ee\u9898\u4e00\uff1a \u4f7f\u7528any_value\u51fd\u6570\u7684\u5b50\u67e5\u8be2\u4f1a\u5bfc\u81f4where in\u5931\u6548\u3002\u56e0\u4e3aany_value\u51fd\u6570\u8fd4\u56de\u4efb\u610f\u4e00\u884c\u5339\u914d\u8bb0\u5f55\u7684\u503c\uff0c\u5b83\u4e0d\u4fdd\u8bc1\u8fd4\u56de\u552f\u4e00\u503c\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4where in\u6bd4\u8f83\u5931\u8d25\u3002 \u95ee\u9898\u4e8c\uff1a \u5982\u679c\u5728\u5b50\u67e5\u8be2\u4e2d\u7701\u7565as id\u522b\u540d\uff0cmysql\u5c06\u65e0\u6cd5\u627e\u5230select id from &#8230;\u8bed\u53e5\u4e2d\u7684id\u5217\uff0c\u56e0\u4e3a\u5b50\u67e5\u8be2\u6ca1\u6709\u547d\u540d\u5176\u7ed3\u679c\u5217\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cmysql\u4f1a\u5c1d\u8bd5\u81ea\u52a8\u751f\u6210\u5217\u540d\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u786e\u5b9a\u7684\u884c\u4e3a\uff0c\u4f8b\u5982\u8fd4\u56de\u6574\u4e2a\u8868\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u95ee\u9898\u4e00\uff1a \u4f7f\u7528group by\u548cmin\u51fd\u6570\u8fd4\u56de\u6bcf\u4e2a\u5206\u7ec4\u7684\u6700\u5c0fid\u503c\uff0c\u4ece\u800c\u786e\u4fdd\u8fd4\u56de\u552f\u4e00\u503c\uff1a select * from test where id in (select min(id) from test group by type); \u767b\u5f55\u540e\u590d\u5236 \u95ee\u9898\u4e8c\uff1a \u663e\u5f0f\u6307\u5b9a\u5b50\u67e5\u8be2\u7ed3\u679c\u5217\u7684\u522b\u540d\uff1a select * 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-7139","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7139","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=7139"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7139\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}