{"id":7827,"date":"2024-11-14T12:51:22","date_gmt":"2024-11-14T04:51:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7827\/"},"modified":"2024-11-14T12:51:22","modified_gmt":"2024-11-14T04:51:22","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e8%81%94%e8%a1%a8%e6%9f%a5%e8%af%a2%e4%bc%98%e5%8c%96%e7%94%a8%e6%88%b7%e6%94%b6%e8%97%8f%e5%86%85%e5%ae%b9%e8%af%bb%e5%8f%96%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7827\/","title":{"rendered":"\u5982\u4f55\u5229\u7528\u8054\u8868\u67e5\u8be2\u4f18\u5316\u7528\u6237\u6536\u85cf\u5185\u5bb9\u8bfb\u53d6\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173128950490448.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528\u8054\u8868\u67e5\u8be2\u4f18\u5316\u7528\u6237\u6536\u85cf\u5185\u5bb9\u8bfb\u53d6\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528\u8054\u8868\u67e5\u8be2\u4f18\u5316\u7528\u6237\u6536\u85cf\u5185\u5bb9\u8bfb\u53d6\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528\u8054\u8868\u67e5\u8be2\u4f18\u5316\u7528\u6237\u6536\u85cf\u5185\u5bb9\u8bfb\u53d6<\/strong><\/p>\n<p>\u5728\u7528\u6237\u6536\u85cf\u529f\u80fd\u4e2d\uff0c\u5305\u542b\u4e09\u4e2a\u6570\u636e\u8868\uff1afavorites\uff08\u6536\u85cf\u6570\u636e\uff09\u3001goods\uff08\u5546\u54c1\uff09\u548c articles\uff08\u6587\u7ae0\uff09\u3002\u4e3a\u4e86\u8bfb\u53d6\u6536\u85cf\u7684\u5185\u5bb9\u6807\u9898\uff0c\u60a8\u9700\u8981\u5728\u4e24\u4e2a\u6570\u636e\u8868\uff08goods \u548c articles\uff09\u4e2d\u67e5\u627e\u76f8\u5e94\u7684\u6761\u76ee\uff0c\u5e76\u6839\u636e\u6536\u85cf\u65e5\u671f\u6309\u964d\u5e8f\u7ed3\u679c\u3002<\/p>\n<p><strong>\u7b2c\u4e8c\u79cd\u65b9\u6cd5 &#8211; \u4f7f\u7528 in \u67e5\u8be2<\/strong><\/p>\n<p>\u4f7f\u7528\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff08\u5c06 cid \u653e\u5165\u6570\u7ec4\u5e76\u4f7f\u7528 in \u67e5\u8be2\u8bfb\u53d6\u6570\u636e\uff09\uff0c\u867d\u7136\u6548\u7387\u8f83\u9ad8\uff0c\u4f46\u5728\u9700\u8981\u5bf9\u6309\u7167 dateline \u6392\u5e8f\u7684\u7ed3\u679c\u8fdb\u884c\u7ec4\u5408\u65f6\u4f1a\u9047\u5230\u56f0\u96be\u3002<\/p>\n<p><strong>\u8054\u8868\u67e5\u8be2\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528\u8054\u8868\u67e5\u8be2\u3002\u8054\u8868\u67e5\u8be2\u53ef\u4ee5\u5c06\u591a\u4e2a\u6570\u636e\u8868\u8fde\u63a5\u8d77\u6765\u5e76\u5408\u5e76\u5176\u6570\u636e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u5c06 favorites \u8868\u4e0e goods \u548c articles \u8868\u5206\u522b\u4f7f\u7528 left join \u8fde\u63a5\u3002<\/p>\n<p><strong>sql \u67e5\u8be2<\/strong><\/p>\n<pre>SELECT IF(goods.id IS NULL, articles.title, goods.title), favorites.*\nFROM favorites\nLEFT JOIN goods\n    ON goods.id = favorites.cid AND favorites.type = 1\nLEFT JOIN articles\n    ON articles.id = favorites.cid AND favorites.type = 2\nORDER BY dateline DESC<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>if \u8bed\u53e5<\/strong><\/p>\n<p>if \u8bed\u53e5\u7528\u4e8e\u6839\u636e type \u5b57\u6bb5\u7684\u503c\u6765\u9009\u62e9\u663e\u793a goods.title \u8fd8\u662f articles.title\u3002\u5f53 type \u7b49\u4e8e 1 \u65f6\uff0c\u663e\u793a\u5546\u54c1\u6807\u9898\uff1b\u5f53 type \u7b49\u4e8e 2 \u65f6\uff0c\u663e\u793a\u6587\u7ae0\u6807\u9898\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u8fd9\u79cd\u8054\u8868\u67e5\u8be2\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\uff1a<\/p>\n<ul>\n<li>\u4ec5\u6267\u884c\u4e00\u6b21\u6570\u636e\u5e93\u67e5\u8be2\u3002<\/li>\n<li>\u6b63\u786e\u5904\u7406\u4e86\u591a\u5bf9\u4e00\u5173\u7cfb\uff0c\u5e76\u57fa\u4e8e dateline \u5217\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528\u8054\u8868\u67e5\u8be2\u4f18\u5316\u7528\u6237\u6536\u85cf\u5185\u5bb9\u8bfb\u53d6\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>\u5229\u7528\u8054\u8868\u67e5\u8be2\u4f18\u5316\u7528\u6237\u6536\u85cf\u5185\u5bb9\u8bfb\u53d6 \u5728\u7528\u6237\u6536\u85cf\u529f\u80fd\u4e2d\uff0c\u5305\u542b\u4e09\u4e2a\u6570\u636e\u8868\uff1afavorites\uff08\u6536\u85cf\u6570\u636e\uff09\u3001goods\uff08\u5546\u54c1\uff09\u548c articles\uff08\u6587\u7ae0\uff09\u3002\u4e3a\u4e86\u8bfb\u53d6\u6536\u85cf\u7684\u5185\u5bb9\u6807\u9898\uff0c\u60a8\u9700\u8981\u5728\u4e24\u4e2a\u6570\u636e\u8868\uff08goods \u548c articles\uff09\u4e2d\u67e5\u627e\u76f8\u5e94\u7684\u6761\u76ee\uff0c\u5e76\u6839\u636e\u6536\u85cf\u65e5\u671f\u6309\u964d\u5e8f\u7ed3\u679c\u3002 \u7b2c\u4e8c\u79cd\u65b9\u6cd5 &#8211; \u4f7f\u7528 in \u67e5\u8be2 \u4f7f\u7528\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff08\u5c06 cid \u653e\u5165\u6570\u7ec4\u5e76\u4f7f\u7528 in \u67e5\u8be2\u8bfb\u53d6\u6570\u636e\uff09\uff0c\u867d\u7136\u6548\u7387\u8f83\u9ad8\uff0c\u4f46\u5728\u9700\u8981\u5bf9\u6309\u7167 dateline \u6392\u5e8f\u7684\u7ed3\u679c\u8fdb\u884c\u7ec4\u5408\u65f6\u4f1a\u9047\u5230\u56f0\u96be\u3002 \u8054\u8868\u67e5\u8be2\u89e3\u51b3\u65b9\u6848 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528\u8054\u8868\u67e5\u8be2\u3002\u8054\u8868\u67e5\u8be2\u53ef\u4ee5\u5c06\u591a\u4e2a\u6570\u636e\u8868\u8fde\u63a5\u8d77\u6765\u5e76\u5408\u5e76\u5176\u6570\u636e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u5c06 favorites \u8868\u4e0e goods \u548c articles \u8868\u5206\u522b\u4f7f\u7528 left join \u8fde\u63a5\u3002 sql \u67e5\u8be2 SELECT IF(goods.id IS NULL, articles.title, goods.title), favorites.* FROM favorites LEFT JOIN goods ON goods.id = favorites.cid AND favorites.type = 1 LEFT JOIN articles ON articles.id [&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-7827","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7827","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=7827"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7827\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}