{"id":26844,"date":"2024-11-24T12:59:18","date_gmt":"2024-11-24T04:59:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26844\/"},"modified":"2024-11-24T12:59:18","modified_gmt":"2024-11-24T04:59:18","slug":"mysql-%e6%9f%a5%e8%af%a2%e8%af%ad%e5%8f%a5%e8%a6%81%e6%80%8e%e4%b9%88%e5%86%99%ef%bc%9f%e5%a6%82%e4%bd%95%e4%bb%8e%e4%b8%a4%e4%b8%aa%e8%a1%a8%e4%b8%ad%e6%9f%a5%e6%89%be%e6%8c%87%e5%ae%9a%e5%88%86-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26844\/","title":{"rendered":"MySQL \u67e5\u8be2\u8bed\u53e5\u8981\u600e\u4e48\u5199\uff1f\u5982\u4f55\u4ece\u4e24\u4e2a\u8868\u4e2d\u67e5\u627e\u6307\u5b9a\u5206\u7c7b\u7684\u4ea7\u54c1\u4fe1\u606f\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111202747778.jpg\" class=\"aligncenter\" title=\"MySQL \u67e5\u8be2\u8bed\u53e5\u8981\u600e\u4e48\u5199\uff1f\u5982\u4f55\u4ece\u4e24\u4e2a\u8868\u4e2d\u67e5\u627e\u6307\u5b9a\u5206\u7c7b\u7684\u4ea7\u54c1\u4fe1\u606f\uff1f\u63d2\u56fe\" alt=\"MySQL \u67e5\u8be2\u8bed\u53e5\u8981\u600e\u4e48\u5199\uff1f\u5982\u4f55\u4ece\u4e24\u4e2a\u8868\u4e2d\u67e5\u627e\u6307\u5b9a\u5206\u7c7b\u7684\u4ea7\u54c1\u4fe1\u606f\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u67e5\u8be2\u8bed\u53e5\u64b0\u5199\u6280\u5de7<\/strong><\/p>\n<p>\u5728\u6570\u636e\u5e93\u7ba1\u7406\u4e2d\uff0c\u7f16\u5199\u9ad8\u6548\u4e14\u51c6\u786e\u7684\u67e5\u8be2\u8bed\u53e5\u81f3\u5173\u91cd\u8981\u3002\u4e3a\u4e86\u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u7b56\u7565\uff1a<\/p>\n<p>\u5982\u9898\u6240\u8ff0\uff0c\u201cmysql \u67e5\u8be2\u8bed\u53e5\u8981\u600e\u4e48\u5199\u201d\uff0c\u6211\u4eec\u4ee5\u60a8\u63d0\u51fa\u7684\u4ea7\u54c1\u67e5\u8be2\u95ee\u9898\u4e3a\u4f8b\u6765\u5c55\u5f00\u56de\u7b54\u3002<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u201c\u4ea7\u54c1\u8868 t_product \u548c \u4ea7\u54c1\u6269\u5c55\u5206\u7c7b\u8868 t_product_category \u4e2d\uff0c\u9700\u8981\u67e5\u627e\u6307\u5b9a category_id \u7684\u4ea7\u54c1\u4fe1\u606f\u3002\u5982\u679c\u5728 t_product \u8868\u4e2d\u672a\u627e\u5230\uff0c\u4f46\u5728 t_product_category \u8868\u4e2d\u5b58\u5728\uff0c\u4e5f\u9700\u8981\u8fd4\u56de\u3002\u5e94\u5982\u4f55\u64b0\u5199\u67e5\u8be2\u8bed\u53e5\uff1f\u201d<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/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><strong>\u89e3\u6790\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>left join\uff1a<\/strong>\u8fde\u63a5 t_product \u548c t_product_category \u8868\uff0c\u5e76\u4fdd\u7559\u5de6\u4fa7\u8868\uff08t_product\uff09\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u53f3\u4fa7\u8868\uff08t_product_category\uff09\u4e2d\u4e0d\u5b58\u5728\u5339\u914d\u7684\u884c\u3002<\/li>\n<li> <strong>where\uff1a<\/strong>\u7b5b\u9009\u6ee1\u8db3 category_id \u4e3a 1 \u6216 2 \u7684\u884c\u3002<\/li>\n<li> <strong>group by\uff1a<\/strong>\u5c06\u7ed3\u679c\u6309 p.product_id \u5206\u7ec4\uff0c\u4ee5\u9632\u6b62\u91cd\u590d\u4ea7\u54c1id\u51fa\u73b0\u3002<\/li>\n<li> <strong>order by\uff1a<\/strong>\u6309 p.seq \u5347\u5e8f\u3001p.product_id \u964d\u5e8f\u7ed3\u679c\u3002<\/li>\n<li> <strong>limit\uff1a<\/strong>\u9650\u5236\u6700\u591a\u8fd4\u56de 20 \u884c\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u91c7\u7528 left join \u5e76\u5728 where \u5b50\u53e5\u4e2d\u7b5b\u9009\u6761\u4ef6\uff0c\u8be5\u67e5\u8be2\u786e\u4fdd\u4ece t_product \u548c t_product_category \u8868\u4e2d\u68c0\u7d22\u6240\u6709\u76f8\u5173\u4ea7\u54c1\uff0c\u4ece\u800c\u89e3\u51b3\u60a8\u7684\u67e5\u8be2\u96be\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u67e5\u8be2\u8bed\u53e5\u8981\u600e\u4e48\u5199\uff1f\u5982\u4f55\u4ece\u4e24\u4e2a\u8868\u4e2d\u67e5\u627e\u6307\u5b9a\u5206\u7c7b\u7684\u4ea7\u54c1\u4fe1\u606f\uff1f\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>\u67e5\u8be2\u8bed\u53e5\u64b0\u5199\u6280\u5de7 \u5728\u6570\u636e\u5e93\u7ba1\u7406\u4e2d\uff0c\u7f16\u5199\u9ad8\u6548\u4e14\u51c6\u786e\u7684\u67e5\u8be2\u8bed\u53e5\u81f3\u5173\u91cd\u8981\u3002\u4e3a\u4e86\u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u7b56\u7565\uff1a \u5982\u9898\u6240\u8ff0\uff0c\u201cmysql \u67e5\u8be2\u8bed\u53e5\u8981\u600e\u4e48\u5199\u201d\uff0c\u6211\u4eec\u4ee5\u60a8\u63d0\u51fa\u7684\u4ea7\u54c1\u67e5\u8be2\u95ee\u9898\u4e3a\u4f8b\u6765\u5c55\u5f00\u56de\u7b54\u3002 \u95ee\u9898\uff1a \u201c\u4ea7\u54c1\u8868 t_product \u548c \u4ea7\u54c1\u6269\u5c55\u5206\u7c7b\u8868 t_product_category \u4e2d\uff0c\u9700\u8981\u67e5\u627e\u6307\u5b9a category_id \u7684\u4ea7\u54c1\u4fe1\u606f\u3002\u5982\u679c\u5728 t_product \u8868\u4e2d\u672a\u627e\u5230\uff0c\u4f46\u5728 t_product_category \u8868\u4e2d\u5b58\u5728\uff0c\u4e5f\u9700\u8981\u8fd4\u56de\u3002\u5e94\u5982\u4f55\u64b0\u5199\u67e5\u8be2\u8bed\u53e5\uff1f\u201d \u7b54\u6848\uff1a SELECT p.* FROM t_product AS p LEFT JOIN t_product_category AS pc ON p.product_id = pc.product_id WHERE p.category_id IN (1,2) OR 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 \u89e3\u6790\uff1a [&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-26844","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26844","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=26844"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26844\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}