{"id":6766,"date":"2024-11-14T14:02:03","date_gmt":"2024-11-14T06:02:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6766\/"},"modified":"2024-11-14T14:02:03","modified_gmt":"2024-11-14T06:02:03","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-sql-%e6%9f%a5%e8%af%a2%e6%96%87%e7%ab%a0%e5%88%97%e8%a1%a8%e5%b9%b6%e5%88%a4%e6%96%ad%e5%bd%93%e5%89%8d%e7%94%a8%e6%88%b7%e6%98%af%e5%90%a6%e7%82%b9%e8%b5%9e","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6766\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8be2\u6587\u7ae0\u5217\u8868\u5e76\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u70b9\u8d5e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172991073675954.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8be2\u6587\u7ae0\u5217\u8868\u5e76\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u70b9\u8d5e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8be2\u6587\u7ae0\u5217\u8868\u5e76\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u70b9\u8d5e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u67e5\u8be2\u6587\u7ae0\u5217\u8868\u5e76\u83b7\u53d6\u662f\u5426\u70b9\u8d5e<\/strong><\/p>\n<p>\u8981\u540c\u65f6\u67e5\u8be2\u6587\u7ae0\u5217\u8868\u548c\u6587\u7ae0\u662f\u5426\u88ab\u70b9\u8d5e\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5916\u8fde\u63a5\uff08left join\uff09\u3002<\/p>\n<p><strong>sql \u67e5\u8be2\u8bed\u53e5<\/strong><\/p>\n<pre>select\n    a.id,\n    a.userid,\n    a.workid,\n    a.name,\n    (\n        select\n            count(*)\n        from \u559c\u6b22\u8868 as l\n        where\n            l.workid = a.workid\n    ) as like\nfrom\n    \u6587\u7ae0\u8868 as a\nleft join\n    \u559c\u6b22\u8868 as l\non\n    a.workid = l.workid<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u67e5\u8be2\u7ed3\u679c<\/strong><\/p>\n<p>\u8fd4\u56de\u7684\u6587\u7ae0\u5217\u8868\u4e2d\u5c06\u5305\u542b\u4ee5\u4e0b\u5217\uff1a<\/p>\n<ul>\n<li>id<\/li>\n<li>userid<\/li>\n<li>workid<\/li>\n<li>name<\/li>\n<li>like\uff1a\u8be5\u6587\u7ae0\u662f\u5426\u88ab\u70b9\u8d5e\uff080\/1\uff09<\/li>\n<\/ul>\n<p><strong>\u5982\u679c\u9700\u8981\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u559c\u6b22<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u559c\u6b22\u67d0\u7bc7\u6587\u7ae0\uff0c\u5219\u9700\u8981\u5728\u67e5\u8be2\u4e2d\u6dfb\u52a0\u5f53\u524d\u7528\u6237 id\u3002<\/p>\n<p><strong>\u4fee\u6539\u540e\u7684 sql \u67e5\u8be2\u8bed\u53e5<\/strong><\/p>\n<pre>SELECT\n    a.id,\n    a.userid,\n    a.workid,\n    a.name,\n    (\n        SELECT\n            COUNT(*)\n        FROM \u559c\u6b22\u8868 AS l\n        WHERE\n            l.workid = a.workid AND l.userid = &lt;\u5f53\u524d\u7528\u6237 ID&gt;\n    ) AS like\nFROM\n    \u6587\u7ae0\u8868 AS a\nLEFT JOIN\n    \u559c\u6b22\u8868 AS l\nON\n    a.workid = l.workid<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8be2\u6587\u7ae0\u5217\u8868\u5e76\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u70b9\u8d5e\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\u67e5\u8be2\u6587\u7ae0\u5217\u8868\u5e76\u83b7\u53d6\u662f\u5426\u70b9\u8d5e \u8981\u540c\u65f6\u67e5\u8be2\u6587\u7ae0\u5217\u8868\u548c\u6587\u7ae0\u662f\u5426\u88ab\u70b9\u8d5e\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5916\u8fde\u63a5\uff08left join\uff09\u3002 sql \u67e5\u8be2\u8bed\u53e5 select a.id, a.userid, a.workid, a.name, ( select count(*) from \u559c\u6b22\u8868 as l where l.workid = a.workid ) as like from \u6587\u7ae0\u8868 as a left join \u559c\u6b22\u8868 as l on a.workid = l.workid \u767b\u5f55\u540e\u590d\u5236 \u67e5\u8be2\u7ed3\u679c \u8fd4\u56de\u7684\u6587\u7ae0\u5217\u8868\u4e2d\u5c06\u5305\u542b\u4ee5\u4e0b\u5217\uff1a id userid workid name like\uff1a\u8be5\u6587\u7ae0\u662f\u5426\u88ab\u70b9\u8d5e\uff080\/1\uff09 \u5982\u679c\u9700\u8981\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u559c\u6b22 \u5982\u679c\u9700\u8981\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u559c\u6b22\u67d0\u7bc7\u6587\u7ae0\uff0c\u5219\u9700\u8981\u5728\u67e5\u8be2\u4e2d\u6dfb\u52a0\u5f53\u524d\u7528\u6237 id\u3002 \u4fee\u6539\u540e\u7684 sql \u67e5\u8be2\u8bed\u53e5 SELECT a.id, a.userid, a.workid, [&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-6766","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6766","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=6766"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6766\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}