{"id":26668,"date":"2024-11-24T10:12:55","date_gmt":"2024-11-24T02:12:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26668\/"},"modified":"2024-11-24T10:12:55","modified_gmt":"2024-11-24T02:12:55","slug":"phpmysql-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%9c%b0%e8%af%bb%e5%8f%96%e7%94%a8%e6%88%b7%e6%94%b6%e8%97%8f%e5%86%85%e5%ae%b9%e5%b9%b6%e6%8c%89%e6%94%b6%e8%97%8f%e6%97%b6%e9%97%b4-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26668\/","title":{"rendered":"PHP+MySQL \u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u8bfb\u53d6\u7528\u6237\u6536\u85cf\u5185\u5bb9\u5e76\u6309\u6536\u85cf\u65f6\u95f4\u6392\u5e8f\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173104128397985.jpg\" class=\"aligncenter\" title=\"PHP+MySQL \u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u8bfb\u53d6\u7528\u6237\u6536\u85cf\u5185\u5bb9\u5e76\u6309\u6536\u85cf\u65f6\u95f4\u6392\u5e8f\uff1f\u63d2\u56fe\" alt=\"PHP+MySQL \u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u8bfb\u53d6\u7528\u6237\u6536\u85cf\u5185\u5bb9\u5e76\u6309\u6536\u85cf\u65f6\u95f4\u6392\u5e8f\uff1f\u63d2\u56fe\" \/><\/p>\n<p>\u5728 php+ \u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u8bfb\u53d6\u7528\u6237\u6536\u85cf\u7684\u5185\u5bb9\uff1f<\/p>\n<p>\u5f53\u6d89\u53ca\u5230\u8bfb\u53d6\u7528\u6237\u6536\u85cf\u7684\u5185\u5bb9\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4ece\u591a\u4e2a\u8868\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u5047\u8bbe\u6709\u4e09\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li> <strong>\u6536\u85cf\u8868 (favorites)<\/strong>\uff1a\u5b58\u50a8\u7528\u6237\u6536\u85cfid\u548c\u65f6\u95f4\u6233\u7684\u8868\u3002<\/li>\n<li> <strong>\u5546\u54c1\u8868 (goods)<\/strong>\uff1a\u5b58\u50a8\u5546\u54c1\u4fe1\u606f\u7684\u8868\u3002<\/li>\n<li> <strong>\u6587\u7ae0\u8868 (articles)<\/strong>\uff1a\u5b58\u50a8\u6587\u7ae0\u4fe1\u606f\u7684\u8868\u3002<\/li>\n<\/ul>\n<p><strong>\u95ee\u9898\uff1a\u8bfb\u53d6\u6309\u6536\u85cf\u65f6\u95f4\u6392\u5e8f\u7684\u6807\u9898<\/strong><\/p>\n<p>\u76ee\u6807\u662f\u83b7\u53d6\u6309\u6536\u85cf\u65f6\u95f4\u964d\u5e8f\u7684\u6536\u85cf\u6807\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff1a\u5faa\u73af\u67e5\u8be2<\/strong><\/p>\n<pre>$favorites = $db-&gt;query('select * from favorites order by dateline desc');\n\nforeach ($favorites as $fav) {\n    if ($fav['type'] == 1) {\n        $title = $db-&gt;query(\"select title from goods where id = {$fav['cid']}\")-&gt;fetchcolumn();\n    } else {\n        $title = $db-&gt;query(\"select title from articles where id = {$fav['cid']}\")-&gt;fetchcolumn();\n    }\n    echo $title;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a\u8054\u8868\u67e5\u8be2<\/strong><\/p>\n<pre>SELECT IF(goods.id IS NULL, articles.title, goods.title) AS title, \n       favorites.* \nFROM favorites\nLEFT JOIN goods ON goods.id = favorites.cid AND favorites.type = 1\nLEFT JOIN articles ON articles.id = favorites.cid AND favorites.type = 2\nORDER BY dateline DESC<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u901a\u8fc7\u8054\u8868\u67e5\u8be2\u540c\u65f6\u83b7\u53d6\u5546\u54c1\u548c\u6587\u7ae0\u6807\u9898\u3002\u7136\u800c\uff0c\u5b83\u9700\u8981\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff0c\u5c06\u6240\u6709\u6807\u9898\u5408\u5e76\u5230\u4e00\u4e2a\u6709\u5e8f\u5217\u8868\u4e2d\u3002\u5982\u679c\u6536\u85cf\u91cf\u8f83\u5927\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p><strong>\u8ba8\u8bba<\/strong><\/p>\n<p>\u4e24\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002\u7b2c\u4e00\u79cd\u65b9\u6cd5\u6d89\u53ca\u66f4\u591a\u7684\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u4f46\u6392\u5e8f\u7b80\u5355\u3002\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u9700\u8981\u66f4\u590d\u6742\u7684\u6392\u5e8f\u903b\u8f91\uff0c\u4f46\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u5e93\u67e5\u8be2\u6570\u91cf\u3002\u9009\u62e9\u6700\u4f73\u65b9\u6cd5\u53d6\u51b3\u4e8e\u7279\u5b9a\u7528\u4f8b\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u6536\u85cf\u7c7b\u578b\u4e0d\u6b62\u4e24\u79cd\uff0c\u5219\u9700\u8981\u4fee\u6539\u67e5\u8be2\u6216\u4f7f\u7528 case when \u8bed\u53e5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPHP+MySQL \u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u8bfb\u53d6\u7528\u6237\u6536\u85cf\u5185\u5bb9\u5e76\u6309\u6536\u85cf\u65f6\u95f4\u6392\u5e8f\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>\u5728 php+ \u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u8bfb\u53d6\u7528\u6237\u6536\u85cf\u7684\u5185\u5bb9\uff1f \u5f53\u6d89\u53ca\u5230\u8bfb\u53d6\u7528\u6237\u6536\u85cf\u7684\u5185\u5bb9\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4ece\u591a\u4e2a\u8868\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u5047\u8bbe\u6709\u4e09\u4e2a\u8868\uff1a \u6536\u85cf\u8868 (favorites)\uff1a\u5b58\u50a8\u7528\u6237\u6536\u85cfid\u548c\u65f6\u95f4\u6233\u7684\u8868\u3002 \u5546\u54c1\u8868 (goods)\uff1a\u5b58\u50a8\u5546\u54c1\u4fe1\u606f\u7684\u8868\u3002 \u6587\u7ae0\u8868 (articles)\uff1a\u5b58\u50a8\u6587\u7ae0\u4fe1\u606f\u7684\u8868\u3002 \u95ee\u9898\uff1a\u8bfb\u53d6\u6309\u6536\u85cf\u65f6\u95f4\u6392\u5e8f\u7684\u6807\u9898 \u76ee\u6807\u662f\u83b7\u53d6\u6309\u6536\u85cf\u65f6\u95f4\u964d\u5e8f\u7684\u6536\u85cf\u6807\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u7b2c\u4e00\u79cd\u65b9\u6cd5\uff1a\u5faa\u73af\u67e5\u8be2 $favorites = $db-&gt;query(&#8216;select * from favorites order by dateline desc&#8217;); foreach ($favorites as $fav) { if ($fav[&#8216;type&#8217;] == 1) { $title = $db-&gt;query(&#8220;select title from goods where id = {$fav[&#8216;cid&#8217;]}&#8221;)-&gt;fetchcolumn(); } else { $title = $db-&gt;query(&#8220;select title from articles where 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-26668","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26668","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=26668"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26668\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}