{"id":7639,"date":"2024-11-14T09:23:01","date_gmt":"2024-11-14T01:23:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7639\/"},"modified":"2024-11-14T09:23:01","modified_gmt":"2024-11-14T01:23:01","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:\/\/fwq.ai\/blog\/7639\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173107440533824.jpg\" class=\"aligncenter\" title=\"\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\u63d2\u56fe\" alt=\"\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\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199  \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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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>\u4ee5\u4e0a\u5c31\u662f\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\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>\u5982\u4f55\u7f16\u5199 \u67e5\u8be2\u8bed\u53e5\u4ee5\u5339\u914d\u4e24\u4e2a\u8868\u4e2d\u7684\u7279\u5b9a\u7c7b\u522b\uff1f \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 \u4ee5\u4e0b\u9519\u8bef\u67e5\u8be2\u793a\u4f8b\uff1a select p.* from t_product as p , t_product_category as pc where ( p.category_id in ( 1,2 ) or ( p.product_id = pc.product_id and pc.category_id in ( 1,2 ) ) ) group by p.product_id order by p.seq asc, p.product_id desc limit 0, 20 \u767b\u5f55\u540e\u590d\u5236 \u6b63\u786e\u7684\u67e5\u8be2\u8bed\u53e5\u5982\u4e0b\uff1a SELECT p.* FROM t_product AS p LEFT [&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-7639","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7639","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=7639"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7639\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}