{"id":200759,"date":"2025-05-06T12:52:38","date_gmt":"2025-05-06T04:52:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200759\/"},"modified":"2025-05-06T12:52:38","modified_gmt":"2025-05-06T04:52:38","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-mysql-%e6%9f%a5%e8%af%a2%e8%af%ad%e5%8f%a5%e5%8c%b9%e9%85%8d%e4%b8%a4%e4%b8%aa%e8%a1%a8%e4%b8%ad%e7%89%b9%e5%ae%9a%e7%b1%bb%e5%88%ab%ef%bc%8c%e5%8d%b3%e4%bd%bf%e4%ba%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200759\/","title":{"rendered":"\u5982\u4f55\u7528 MySQL \u67e5\u8be2\u8bed\u53e5\u5339\u914d\u4e24\u4e2a\u8868\u4e2d\u7279\u5b9a\u7c7b\u522b\uff0c\u5373\u4f7f\u4ea7\u54c1\u6ca1\u6709\u76f4\u63a5\u5173\u8054\u5230\u8be5\u7c7b\u522b\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u7528 MySQL \u67e5\u8be2\u8bed\u53e5\u5339\u914d\u4e24\u4e2a\u8868\u4e2d\u7279\u5b9a\u7c7b\u522b\uff0c\u5373\u4f7f\u4ea7\u54c1\u6ca1\u6709\u76f4\u63a5\u5173\u8054\u5230\u8be5\u7c7b\u522b\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6570\u636e\u5e93\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u7528 MySQL \u67e5\u8be2\u8bed\u53e5\u5339\u914d\u4e24\u4e2a\u8868\u4e2d\u7279\u5b9a\u7c7b\u522b\uff0c\u5373\u4f7f\u4ea7\u54c1\u6ca1\u6709\u76f4\u63a5\u5173\u8054\u5230\u8be5\u7c7b\u522b\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241113\/173148953267346efc43d42.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199 mysql \u67e5\u8be2\u8bed\u53e5\u4ee5\u5339\u914d\u4e24\u4e2a\u8868\u4e2d\u7684\u7279\u5b9a\u7c7b\u522b\uff1f<\/strong><\/p>\n<p>\u5728\u4ea7\u54c1\u8868\u548c\u4ea7\u54c1\u6269\u5c55\u5206\u7c7b\u8868\u4e2d\uff0c\u4ea7\u54c1\u53ef\u80fd\u5177\u6709\u591a\u4e2a\u6269\u5c55\u5206\u7c7b\u3002\u5982\u4f55\u7f16\u5199\u67e5\u8be2\u8bed\u53e5\u4ee5\u83b7\u53d6\u6839\u636e\u7279\u5b9a\u7c7b\u522b\u67e5\u627e\u7684\u4ea7\u54c1\uff0c\u5373\u4f7f\u8be5\u4ea7\u54c1\u6ca1\u6709\u76f4\u63a5\u5173\u8054\u5230\u8be5\u7c7b\u522b\u4e5f\u53ef\u4ee5\u67e5\u627e\u51fa\u6765\uff1f<\/p>\n<p>\u4ee5\u4e0b\u9519\u8bef\u67e5\u8be2\u793a\u4f8b\uff1a<\/p>\n<pre>select p.*\nfrom t_product as p , t_product_category as pc\nwhere ( p.category_id in ( 1,2 ) or ( p.product_id = pc.product_id and pc.category_id in ( 1,2 ) ) )\ngroup by p.product_id\norder by p.seq asc, p.product_id desc\nlimit 0, 20<\/pre>\n<p>\u6b63\u786e\u7684\u67e5\u8be2\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT p.*\nFROM t_product AS p \nLEFT JOIN t_product_category AS pc ON p.product_id = pc.product_id \nWHERE p.category_id IN (1,2) OR pc.category_id IN (1,2) \nGROUP BY p.product_id \nORDER BY p.seq ASC, p.product_id DESC \nLIMIT 0, 20<\/pre>\n<p>\u6b64\u67e5\u8be2\u4f7f\u7528 left join \u6765\u5339\u914d t_product \u548c t_product_category \u8868\uff0c\u5e76\u4f7f\u7528 or \u6761\u4ef6\u6765\u83b7\u53d6\u7b26\u5408 category_id \u6807\u51c6\u7684\u8bb0\u5f55\u3002\u5373\u4f7f\u8be5\u4ea7\u54c1\u672c\u8eab\u6ca1\u6709\u76f4\u63a5\u5173\u8054\u5230\u8be5\u7c7b\u522b\uff0c\u4e5f\u53ef\u4ee5\u67e5\u627e\u51fa\u5177\u6709\u6269\u5c55\u5206\u7c7b\u7684\u4ea7\u54c1\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u7528 MySQL \u67e5\u8be2\u8bed\u53e5\u5339\u914d\u4e24\u4e2a\u8868\u4e2d\u7279\u5b9a\u7c7b\u522b\uff0c\u5373\u4f7f\u4ea7\u54c1\u6ca1\u6709\u76f4\u63a5\u5173\u8054\u5230\u8be5\u7c7b\u522b\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528 MySQL \u67e5\u8be2\u8bed\u53e5\u5339\u914d&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200759","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200759"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200759\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}