{"id":23197,"date":"2024-11-21T10:40:07","date_gmt":"2024-11-21T02:40:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23197\/"},"modified":"2024-11-21T10:40:07","modified_gmt":"2024-11-21T02:40:07","slug":"thinkphp5%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e9%87%8d%e5%a4%8d%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23197\/","title":{"rendered":"thinkphp5\u5982\u4f55\u53bb\u9664\u91cd\u590d\u67e5\u8be2"},"content":{"rendered":"<\/p>\n<p><strong>\u4e00\u3001\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>\u5728 SQL \u8bed\u53e5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57\u6765\u83b7\u53d6\u4e0d\u91cd\u590d\u7684\u6570\u636e\u3002\u5728 ThinkPHP5 \u4e2d\uff0c\u4f7f\u7528 hinkDb \u7c7b\u7684 query() \u65b9\u6cd5\u6267\u884c SQL \u67e5\u8be2\u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u67e5\u8be2\u6761\u4ef6\u4e2d\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>use&nbsp;\thinkDb;\n\n$data&nbsp;=&nbsp;Db::query('SELECT&nbsp;DISTINCT&nbsp;`brand`&nbsp;FROM&nbsp;`goods`&nbsp;WHERE&nbsp;`category_id`&nbsp;=&nbsp;1');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 SELECT DISTINCT \u5173\u952e\u5b57\u83b7\u53d6\u4e86 goods \u8868\u4e2d category_id \u4e3a 1 \u7684\u6240\u6709\u54c1\u724c\uff0c\u5e76\u5c06\u67e5\u8be2\u7ed3\u679c\u8d4b\u503c\u7ed9\u4e86 $data \u53d8\u91cf\u3002<\/p>\n<p><strong>\u4e8c\u3001\u4f7f\u7528 distinct() \u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u9664\u4e86\u5728 SELECT \u8bed\u53e5\u4e2d\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 ThinkPHP5 \u63d0\u4f9b\u7684 distinct() \u65b9\u6cd5\u5b9e\u73b0\u53bb\u91cd\u67e5\u8be2\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>use&nbsp;\thinkDb;\n\n$data&nbsp;=&nbsp;Db::name('goods')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;where('category_id',&nbsp;1)\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;distinct(true)\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;field('brand')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;select();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 Db \u7c7b\u7684 name() \u65b9\u6cd5\u6307\u5b9a\u4e86\u67e5\u8be2\u7684\u6570\u636e\u8868\uff0c\u4f7f\u7528\u4e86 where() \u65b9\u6cd5\u6307\u5b9a\u4e86\u7b5b\u9009\u6761\u4ef6\uff0c\u4f7f\u7528\u4e86 distinct() \u65b9\u6cd5\u5f00\u542f\u4e86\u53bb\u91cd\u67e5\u8be2\uff0c\u4f7f\u7528\u4e86 field() \u65b9\u6cd5\u6307\u5b9a\u67e5\u8be2\u7684\u5b57\u6bb5\uff0c\u6700\u540e\u4f7f\u7528\u4e86 select() \u65b9\u6cd5\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4e09\u3001\u4f7f\u7528 group() \u65b9\u6cd5<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57\u548c distinct() \u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 group() \u65b9\u6cd5\u5b9e\u73b0\u53bb\u91cd\u67e5\u8be2\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>use&nbsp;\thinkDb;\n\n$data&nbsp;=&nbsp;Db::name('goods')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;where('category_id',&nbsp;1)\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;group('brand')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;select();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 Db \u7c7b\u7684 name() \u65b9\u6cd5\u6307\u5b9a\u4e86\u67e5\u8be2\u7684\u6570\u636e\u8868\uff0c\u4f7f\u7528\u4e86 where() \u65b9\u6cd5\u6307\u5b9a\u4e86\u7b5b\u9009\u6761\u4ef6\uff0c\u4f7f\u7528\u4e86 group() \u65b9\u6cd5\u8fdb\u884c\u5206\u7ec4\uff0c\u6700\u540e\u4f7f\u7528\u4e86 select() \u65b9\u6cd5\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u4e1a\u52a1\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u53bb\u91cd\u67e5\u8be2\u65b9\u6cd5\uff0c\u4ee5\u4fdd\u8bc1\u67e5\u8be2\u7ed3\u679c\u7684\u6b63\u786e\u6027\u548c\u6548\u7387\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f5\u5982\u4f55\u53bb\u9664\u91cd\u590d\u67e5\u8be2\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57 \u5728 SQL \u8bed\u53e5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57\u6765\u83b7\u53d6\u4e0d\u91cd\u590d\u7684\u6570\u636e\u3002\u5728 ThinkPHP5 \u4e2d\uff0c\u4f7f\u7528 hinkDb \u7c7b\u7684 query() \u65b9\u6cd5\u6267\u884c SQL \u67e5\u8be2\u8bed\u53e5\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u67e5\u8be2\u6761\u4ef6\u4e2d\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1a use&nbsp; hinkDb; $data&nbsp;=&nbsp;Db::query(&#8216;SELECT&nbsp;DISTINCT&nbsp;`brand`&nbsp;FROM&nbsp;`goods`&nbsp;WHERE&nbsp;`category_id`&nbsp;=&nbsp;1&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 SELECT DISTINCT \u5173\u952e\u5b57\u83b7\u53d6\u4e86 goods \u8868\u4e2d category_id \u4e3a 1 \u7684\u6240\u6709\u54c1\u724c\uff0c\u5e76\u5c06\u67e5\u8be2\u7ed3\u679c\u8d4b\u503c\u7ed9\u4e86 $data \u53d8\u91cf\u3002 \u4e8c\u3001\u4f7f\u7528 distinct() \u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u9664\u4e86\u5728 SELECT \u8bed\u53e5\u4e2d\u4f7f\u7528 SELECT DISTINCT \u5173\u952e\u5b57\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 ThinkPHP5 \u63d0\u4f9b\u7684 distinct() \u65b9\u6cd5\u5b9e\u73b0\u53bb\u91cd\u67e5\u8be2\u3002\u5982\u4e0b\u6240\u793a\uff1a use&nbsp; hinkDb; $data&nbsp;=&nbsp;Db::name(&#8216;goods&#8217;) &nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;where(&#8216;category_id&#8217;,&nbsp;1) [&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-23197","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23197","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=23197"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23197\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}