{"id":48077,"date":"2024-12-02T09:33:04","date_gmt":"2024-12-02T01:33:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48077\/"},"modified":"2024-12-02T09:33:04","modified_gmt":"2024-12-02T01:33:04","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87-sql-%e6%9f%a5%e8%af%a2%e8%8e%b7%e5%8f%96%e6%96%87%e7%ab%a0%e5%88%97%e8%a1%a8%e5%b9%b6%e6%9f%a5%e8%af%a2%e5%bd%93%e5%89%8d%e7%94%a8%e6%88%b7%e7%9a%84%e7%82%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48077\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7 SQL \u67e5\u8be2\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u70b9\u8d5e\u72b6\u6001\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u901a\u8fc7 SQL \u67e5\u8be2\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u70b9\u8d5e\u72b6\u6001\uff1f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u901a\u8fc7 SQL \u67e5\u8be2\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u70b9\u8d5e\u72b6\u6001\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729954847671d041fcadac.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u901a\u8fc7 SQL \u67e5\u8be2\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u70b9\u8d5e\u72b6\u6001\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u901a\u8fc7 SQL \u67e5\u8be2\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u70b9\u8d5e\u72b6\u6001\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u70b9\u8d5e\u72b6\u6001<\/strong><\/p>\n<p>\u5728\u5f00\u53d1\u793e\u533a\u6216\u535a\u5ba2\u7cfb\u7edf\u4e2d\uff0c\u9700\u8981\u5c55\u793a\u6587\u7ae0\u5217\u8868\u7684\u540c\u65f6\uff0c\u8fd8\u8981\u67e5\u8be2\u6587\u7ae0\u662f\u5426\u88ab\u5f53\u524d\u7528\u6237\u70b9\u8d5e\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u5982\u4f55\u901a\u8fc7 sql \u67e5\u8be2\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<p><strong>\u67e5\u8be2\u8bed\u53e5<\/strong><\/p>\n<p>\u4e3a\u4e86\u540c\u65f6\u67e5\u8be2\u6587\u7ae0\u5217\u8868\u548c\u70b9\u8d5e\u72b6\u6001\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684 sql \u8bed\u53e5\uff1a<\/p>\n<pre>select id, userid, workid, name, \n    (select count(*) from \u559c\u6b22\u8868 as l where l.workid = a.workid) as like \n    from \u6587\u7ae0\u8868 as a<\/pre>\n<p><strong>\u8868\u7ed3\u6784<\/strong><\/p>\n<ul>\n<li><strong>\u6587\u7ae0\u8868<\/strong>: \u5305\u542b id, userid, workid \u548c name \u5b57\u6bb5\u3002<\/li>\n<li><strong>\u559c\u6b22\u8868<\/strong>: \u5305\u542b id, userid \u548c workid \u5b57\u6bb5\uff0c\u5176\u4e2d workid \u662f\u88ab\u70b9\u8d5e\u7684\u6587\u7ae0\u7684 id\u3002<\/li>\n<\/ul>\n<p><strong>\u67e5\u8be2\u6b65\u9aa4<\/strong><\/p>\n<p>\u8be5\u67e5\u8be2\u8bed\u53e5\u9996\u5148\u4ece\u6587\u7ae0\u8868\u4e2d\u9009\u62e9\u5fc5\u8981\u7684\u5b57\u6bb5\uff0c\u5305\u62ec id, userid, workid \u548c name\u3002<\/p>\n<p>\u7136\u540e\uff0c\u4f7f\u7528\u5b50\u67e5\u8be2\u6765\u68c0\u67e5\u6bcf\u4e00\u7bc7\u6587\u7ae0\u662f\u5426\u88ab\u7528\u6237\u70b9\u8d5e\u3002\u5b50\u67e5\u8be2\u4ece\u559c\u6b22\u8868\u4e2d\u8ba1\u6570\u8be5\u6587\u7ae0 id \uff08l.workid = a.workid\uff09\u7684\u8bb0\u5f55\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728 like \u5217\u4e2d\u3002<\/p>\n<p><strong>\u7ed3\u679c<\/strong><\/p>\n<p>\u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u7bc7\u6587\u7ae0\u7684\u4fe1\u606f\uff0c\u5305\u62ec id, userid, workid, name \u548c like\u3002like \u5217\u7684\u503c\u4e3a 0 \u8868\u793a\u6587\u7ae0\u672a\u88ab\u70b9\u8d5e\uff0c\u4e3a 1 \u8868\u793a\u6587\u7ae0\u5df2\u88ab\u70b9\u8d5e\u3002<\/p>\n<p><strong>\u7528\u6237\u662f\u5426\u70b9\u8d5e<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u70b9\u8d5e\uff0c\u5219\u5fc5\u987b\u8fdb\u884c\u4e24\u6b21\u67e5\u8be2\u3002\u7b2c\u4e00\u6b21\u67e5\u8be2\u8fd4\u56de\u6587\u7ae0\u5217\u8868\uff0c\u83b7\u53d6\u6587\u7ae0 id\u3002\u7b2c\u4e8c\u6b21\u67e5\u8be2\u4f7f\u7528\u8fd9\u4e9b\u6587\u7ae0 id \u4ece\u559c\u6b22\u8868\u4e2d\u67e5\u8be2\u70b9\u8d5e\u6570\u636e\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u81ea\u5df1\u8ba1\u7b97\u6574\u5408\uff0c\u4ee5\u786e\u5b9a\u5f53\u524d\u7528\u6237\u662f\u5426\u70b9\u8d5e\u4e86\u7279\u5b9a\u7684\u6587\u7ae0\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u901a\u8fc7 SQL \u67e5\u8be2\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u70b9\u8d5e\u72b6\u6001\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1aphp \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u901a\u8fc7 SQL \u67e5\u8be2\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u70b9\u8d5e\u72b6\u6001\uff1f \u6536\u85cf \u4f60\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\u300a\u5982\u4f55\u901a\u8fc7 SQL \u67e5\u8be2\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u5f53\u524d\u7528\u6237\u7684\u70b9\u8d5e\u72b6\u6001\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01 \u5982\u4f55\u83b7\u53d6\u6587\u7ae0\u5217\u8868\u5e76\u67e5\u8be2\u70b9\u8d5e\u72b6\u6001 \u5728\u5f00\u53d1\u793e\u533a\u6216\u535a\u5ba2\u7cfb\u7edf\u4e2d\uff0c\u9700\u8981\u5c55\u793a\u6587\u7ae0\u5217\u8868\u7684\u540c\u65f6\uff0c\u8fd8\u8981\u67e5\u8be2\u6587\u7ae0\u662f\u5426\u88ab\u5f53\u524d\u7528\u6237\u70b9\u8d5e\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u5982\u4f55\u901a\u8fc7 sql \u67e5\u8be2\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002 \u67e5\u8be2\u8bed\u53e5 \u4e3a\u4e86\u540c\u65f6\u67e5\u8be2\u6587\u7ae0\u5217\u8868\u548c\u70b9\u8d5e\u72b6\u6001\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684 sql \u8bed\u53e5\uff1a select id, userid, workid, name, (select count(*) from \u559c\u6b22\u8868 as l where l.workid = a.workid) as like from \u6587\u7ae0\u8868 as a \u8868\u7ed3\u6784 \u6587\u7ae0\u8868: \u5305\u542b id, userid, workid \u548c name \u5b57\u6bb5\u3002 \u559c\u6b22\u8868: \u5305\u542b id, userid \u548c workid \u5b57\u6bb5\uff0c\u5176\u4e2d workid \u662f\u88ab\u70b9\u8d5e\u7684\u6587\u7ae0\u7684 id\u3002 [&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-48077","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48077","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=48077"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48077\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}