{"id":307582,"date":"2024-01-18T03:27:13","date_gmt":"2024-01-17T19:27:13","guid":{"rendered":"https:\/\/www.idc.net\/help\/307582\/"},"modified":"2024-01-18T03:27:13","modified_gmt":"2024-01-17T19:27:13","slug":"%e5%9f%ba%e4%ba%8esql%e7%9a%84%e6%95%b0%e6%8d%ae%e5%8f%af%e8%a7%86%e5%8c%96%e5%92%8c%e6%95%b0%e6%8d%ae%e6%8c%96%e6%8e%98","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/307582\/","title":{"rendered":"\u57fa\u4e8eSQL\u7684\u6570\u636e\u53ef\u89c6\u5316\u548c\u6570\u636e\u6316\u6398"},"content":{"rendered":"<p style=\"text-align: center\">\n<p style=\"text-align:center\">\n<\/p>\n<p style=\"text-align: justify\"><span style=\"color: #374151\">\u57fa\u4e8eSQL\u7684\u6570\u636e\u53ef\u89c6\u5316\u548c\u6570\u636e\u6316\u6398\u662f\u76ee\u524d\u4e1a\u5185\u975e\u5e38\u6d41\u884c\u7684\u4e00\u79cd\u6570\u636e\u5206\u6790\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f01\u4e1a\u5feb\u901f\u5730\u4e86\u89e3\u548c\u5206\u6790\u81ea\u8eab\u7684\u6570\u636e\uff0c\u4ece\u800c\u5236\u5b9a\u66f4\u52a0\u79d1\u5b66\u548c\u6709\u6548\u7684\u4e1a\u52a1\u51b3\u7b56\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u4ee5\u4e00\u4e2a\u5b9e\u9645\u7684\u9879\u76ee\u4e3a\u4f8b\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u57fa\u4e8eSQL\u7684\u6570\u636e\u53ef\u89c6\u5316\u548c\u6570\u636e\u6316\u6398\u7684\u6d41\u7a0b\u548c\u6280\u5de7\u3002<\/span><\/p>\n<h2>\u9879\u76ee\u4ecb\u7ecd<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #374151\">\u6211\u4eec\u7684\u9879\u76ee\u662f\u4e00\u4e2a\u5728\u7ebf\u6559\u80b2\u5e73\u53f0\uff0c\u5e73\u53f0\u4e0a\u6709\u6570\u767e\u95e8\u8bfe\u7a0b\uff0c\u6570\u5343\u540d\u5b66\u751f\u5728\u7ebf\u5b66\u4e60\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u4ece\u6570\u636e\u4e2d\u6316\u6398\u51fa\u6709\u4ef7\u503c\u7684\u4fe1\u606f\uff0c\u5e2e\u52a9\u5e73\u53f0\u505a\u51fa\u66f4\u52a0\u79d1\u5b66\u548c\u6709\u6548\u7684\u8fd0\u8425\u51b3\u7b56\u3002<\/span><\/p>\n<h2>\u6570\u636e\u5206\u6790\u6d41\u7a0b<\/h2>\n<h2>1\u3001\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #374151\">\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u9884\u5904\u7406\u3002\u8fd9\u4e00\u6b65\u7684\u4e3b\u8981\u76ee\u7684\u662f\u53bb\u9664\u65e0\u6548\u6570\u636e\uff0c\u586b\u8865\u7f3a\u5931\u503c\uff0c\u5904\u7406\u5f02\u5e38\u503c\u7b49\u3002\u5728\u6211\u4eec\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5b66\u751f\u7684\u5b66\u4e60\u8bb0\u5f55\u8fdb\u884c\u5904\u7406\uff0c\u5305\u62ec\u8bfe\u7a0b\u540d\u79f0\uff0c\u5b66\u751f\u59d3\u540d\uff0c\u5b66\u4e60\u65f6\u957f\uff0c\u5b66\u4e60\u72b6\u6001\u7b49\u4fe1\u606f\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #374151\">\u793a\u4f8bSQL\u8bed\u53e5\uff1a<\/span><\/p>\n<pre><span style=\"color: #d73a49\">--<\/span> <span>\u53bb\u9664\u65e0\u6548\u6570\u636e<\/span><br><span>DELETE<\/span> <span>FROM<\/span> <span>study_record<\/span> <span>WHERE<\/span> <span>course_id<\/span> <span>IS<\/span> <span>NULL<\/span> <span>OR<\/span> <span>user_id<\/span> <span>IS<\/span> <span>NULL<\/span>;<br><br><span style=\"color: #d73a49\">--<\/span> <span>\u586b\u8865\u7f3a\u5931\u503c<\/span><br><span>UPDATE<\/span> <span>study_record<\/span> <span>SET<\/span> <span>learn_time<\/span> <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #005cc5\">0<\/span> <span>WHERE<\/span> <span>learn_time<\/span> <span>IS<\/span> <span>NULL<\/span>;<br><br><span style=\"color: #d73a49\">--<\/span> <span>\u5904\u7406\u5f02\u5e38\u503c<\/span><br><span>UPDATE<\/span> <span>study_record<\/span> <span>SET<\/span> <span>learn_time<\/span> <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #005cc5\">0<\/span> <span>WHERE<\/span> <span>learn_time<\/span> <span style=\"color: #d73a49\">&lt;<\/span> <span style=\"color: #005cc5\">0<\/span>;<\/pre>\n<h2>2\u3001\u6570\u636e\u63a2\u7d22\u548c\u53ef\u89c6\u5316<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #374151\">\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u63a2\u7d22\u548c\u53ef\u89c6\u5316\u3002\u8fd9\u4e00\u6b65\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4e86\u89e3\u6570\u636e\u7684\u5206\u5e03\u89c4\u5f8b\u548c\u76f8\u5173\u6027\uff0c\u4ece\u800c\u4e3a\u540e\u7eed\u7684\u5206\u6790\u63d0\u4f9b\u57fa\u7840\u3002\u5728\u6211\u4eec\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u63a2\u7d22\u5b66\u751f\u7684\u5b66\u4e60\u60c5\u51b5\uff0c\u5305\u62ec\u5b66\u4e60\u65f6\u957f\uff0c\u5b66\u4e60\u72b6\u6001\u7b49\u4fe1\u606f\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #374151\">\u793a\u4f8bSQL\u8bed\u53e5\uff1a<\/span><\/p>\n<pre><span style=\"color: #d73a49\">--<\/span> <span>\u67e5\u8be2\u5b66\u751f\u7684\u5b66\u4e60\u60c5\u51b5<\/span><br><span>SELECT<\/span> <br>    <span>user_id<\/span>,<br>    <span>course_id<\/span>,<br>    <span>SUM<\/span>(<span>learn_time<\/span>) <span>AS<\/span> <span>total_learn_time<\/span>,<br>    <span>COUNT<\/span>(<span>CASE<\/span> <span>WHEN<\/span> <span>status<\/span> <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #690\">'completed'<\/span> <span>THEN<\/span> <span style=\"color: #005cc5\">1<\/span> <span>ELSE<\/span> <span>NULL<\/span> <span>END<\/span>) <span>AS<\/span> <span>completed_count<\/span>,<br>    <span>COUNT<\/span>(<span>CASE<\/span> <span>WHEN<\/span> <span>status<\/span> <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #690\">'in_progress'<\/span> <span>THEN<\/span> <span style=\"color: #005cc5\">1<\/span> <span>ELSE<\/span> <span>NULL<\/span> <span>END<\/span>) <span>AS<\/span> <span>in_progress_count<\/span>,<br>    <span>COUNT<\/span>(<span>CASE<\/span> <span>WHEN<\/span> <span>status<\/span> <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #690\">'not_started'<\/span> <span>THEN<\/span> <span style=\"color: #005cc5\">1<\/span> <span>ELSE<\/span> <span>NULL<\/span> <span>END<\/span>) <span>AS<\/span> <span>not_started_count<\/span><br><span>FROM<\/span> <br>    <span>study_record<\/span><br><span>GROUP<\/span> <span>BY<\/span> <br>    <span>user_id<\/span>,<br>    <span>course_id<\/span>;<\/pre>\n<h2>3\u3001\u6570\u636e\u5efa\u6a21\u548c\u9884\u6d4b<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #374151\">\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u5efa\u6a21\u548c\u9884\u6d4b\u3002\u8fd9\u4e00\u6b65\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5229\u7528\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5efa\u6a21\u548c\u9884\u6d4b\uff0c\u4ece\u800c\u4e3a\u540e\u7eed\u7684\u51b3\u7b56\u63d0\u4f9b\u652f\u6301\u3002\u5728\u6211\u4eec\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u5bf9\u5b66\u751f\u7684\u5b66\u4e60\u884c\u4e3a\u8fdb\u884c\u9884\u6d4b\uff0c\u4ece\u800c\u6839\u636e\u5b66\u751f\u7684\u5b66\u4e60\u884c\u4e3a\u5236\u5b9a\u4e2a\u6027\u5316\u7684\u63a8<\/span>\u8350\u8ba1\u5212\uff0c\u63d0\u9ad8\u5b66\u751f\u7684\u5b66\u4e60\u6548\u679c\u548c\u6ee1\u610f\u5ea6\u3002<\/p>\n<p style=\"text-align: justify\">\u793a\u4f8bSQL\u8bed\u53e5\uff1a<\/p>\n<pre><span style=\"color: #d73a49\">--<\/span> <span>\u5229\u7528\u903b\u8f91\u56de\u5f52\u6a21\u578b\u5bf9\u5b66\u751f\u7684\u5b66\u4e60\u884c\u4e3a\u8fdb\u884c\u9884\u6d4b<\/span><br><span style=\"color: #d73a49\">--<\/span> <span>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u5efa\u7acb\u597d\u4e86\u4e00\u4e2a\u5b66\u4e60\u884c\u4e3a\u9884\u6d4b\u6a21\u578b\uff0c\u6a21\u578b\u4e2d\u5305\u542b\u4ee5\u4e0b\u7279\u5f81\uff1a\u5b66\u4e60\u65f6\u957f\u3001\u5b66\u4e60\u72b6\u6001\u3001\u8bfe\u7a0b\u96be\u5ea6\u7b49<\/span><br><span>SELECT<\/span> <br>    <span>user_id<\/span>,<br>    <span>course_id<\/span>,<br>    <span>CASE<\/span> <span>WHEN<\/span> <span>predicted_label<\/span> <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #005cc5\">1<\/span> <span>THEN<\/span> <span style=\"color: #690\">'recommended'<\/span> <span>ELSE<\/span> <span style=\"color: #690\">'not recommended'<\/span> <span>END<\/span> <span>AS<\/span> <span>recommendation<\/span><br><span>FROM<\/span> <br>    <span>study_record<\/span><br><span>JOIN<\/span> <br>    (<span>SELECT<\/span> <br>        <span>user_id<\/span>,<br>        <span>course_id<\/span>,<br>        <span>predicted_label<\/span><br>     <span>FROM<\/span> <br>        <span>study_behavior_prediction<\/span>) <span>AS<\/span> <span>prediction_table<\/span><br><span>ON<\/span> <br>    <span>study_record<\/span>.<span>user_id<\/span> <span style=\"color: #d73a49\">=<\/span> <span>prediction_table<\/span>.<span>user_id<\/span> <span>AND<\/span> <span>study_record<\/span>.<span>course_id<\/span> <span style=\"color: #d73a49\">=<\/span> <span>prediction_table<\/span>.<span>course_id<\/span>;<\/pre>\n<h2>\u7ed3\u8bed<\/h2>\n<p style=\"text-align: justify\">\u4ee5\u4e0a\u5c31\u662f\u57fa\u4e8eSQL\u7684\u6570\u636e\u53ef\u89c6\u5316\u548c\u6570\u636e\u6316\u6398\u7684\u8be6\u7ec6\u8bb2\u89e3\u548c\u5b9e\u9645\u9879\u76ee\u793a\u4f8b\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5206\u6790\u7684\u5177\u4f53\u6d41\u7a0b\u548c\u6280\u5de7\u53ef\u80fd\u56e0\u9879\u76ee\u548c\u9700\u6c42\u800c\u5f02\uff0c\u4f46\u603b\u7684\u6765\u8bf4\uff0c\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u3001\u6570\u636e\u63a2\u7d22\u548c\u53ef\u89c6\u5316\u3001\u6570\u636e\u5efa\u6a21\u548c\u9884\u6d4b\u662f\u6570\u636e\u5206\u6790\u7684\u4e09\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002\u901a\u8fc7\u719f\u7ec3\u638c\u63e1SQL\u8bed\u8a00\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u548c\u51c6\u786e\u5730\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u4ece\u800c\u5e2e\u52a9\u4f01\u4e1a\u5236\u5b9a\u66f4\u52a0\u79d1\u5b66\u548c\u6709\u6548\u7684\u4e1a\u52a1\u51b3\u7b56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u4e8eSQL\u7684\u6570\u636e\u53ef\u89c6\u5316\u548c\u6570\u636e\u6316\u6398\u662f\u76ee\u524d\u4e1a\u5185\u975e\u5e38\u6d41\u884c\u7684\u4e00\u79cd\u6570\u636e\u5206\u6790\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f01\u4e1a\u5feb\u901f\u5730\u4e86\u89e3\u548c\u5206\u6790\u81ea\u8eab\u7684\u6570\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":307583,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-307582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/307582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=307582"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/307582\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/307583"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=307582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=307582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=307582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}