{"id":7223,"date":"2024-11-14T10:44:16","date_gmt":"2024-11-14T02:44:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7223\/"},"modified":"2024-11-14T10:44:16","modified_gmt":"2024-11-14T02:44:16","slug":"%e5%a6%82%e4%bd%95%e7%94%a8sql%e8%af%ad%e5%8f%a5%e5%af%b9%e8%af%84%e4%bb%b7%e6%95%b0%e6%8d%ae%e8%bf%9b%e8%a1%8c%e5%88%86%e7%bb%84%e7%bb%9f%e8%ae%a1%ef%bc%8c%e5%b9%b6%e8%ae%a1%e7%ae%97%e5%a5%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7223\/","title":{"rendered":"\u5982\u4f55\u7528SQL\u8bed\u53e5\u5bf9\u8bc4\u4ef7\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff0c\u5e76\u8ba1\u7b97\u597d\u8bc4\u7387\u548c\u5747\u5206\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173061974185001.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528SQL\u8bed\u53e5\u5bf9\u8bc4\u4ef7\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff0c\u5e76\u8ba1\u7b97\u597d\u8bc4\u7387\u548c\u5747\u5206\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528SQL\u8bed\u53e5\u5bf9\u8bc4\u4ef7\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff0c\u5e76\u8ba1\u7b97\u597d\u8bc4\u7387\u548c\u5747\u5206\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u8bc4\u4ef7\u5206\u7ec4\u7edf\u8ba1\u4e4b sql \u5b9e\u8df5<\/strong><\/p>\n<p>\u5bf9\u4e8e\u590d\u6742\u7684\u8bc4\u4ef7\u6570\u636e\uff0c\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff0c\u4ee5\u63d0\u53d6\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002\u672c\u6587\u65e8\u5728\u901a\u8fc7 sql \u8bed\u53e5\uff0c\u5206\u522b\u7edf\u8ba1\u5404\u7b49\u7ea7\u7684\u6761\u6570\u3001\u603b\u8bc4\u8bba\u6761\u6570\u3001\u597d\u8bc4\u7387\u548c\u5747\u5206\u3002<\/p>\n<p><strong>\u5404\u7b49\u7ea7\u6761\u6570<\/strong><\/p>\n<p>\u539f\u8868\u5b57\u6bb5\u5df2\u5305\u542b\u5404\u7b49\u7ea7\u6761\u6570\uff0c\u65e0\u9700\u989d\u5916\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u603b\u8bc4\u8bba\u6761\u6570<\/strong><\/p>\n<p>\u904d\u5386\u8ba1\u7b97\u6240\u6709\u8bc4\u8bba\u6761\u6570\uff0c\u6216\u5355\u72ec\u6267\u884c sql \u8bed\u53e5 select count(1) from table_name\u3002<\/p>\n<p><strong>\u597d\u8bc4\u7387<\/strong><\/p>\n<p>\u597d\u8bc4\u7387\u8ba1\u7b97\u516c\u5f0f\uff1a\u597d\u8bc4\u6570 \/ \u603b\u8bc4\u8bba\u6761\u6570\u3002<\/p>\n<p><strong>\u5747\u5206<\/strong><\/p>\n<p>\u5747\u5206\u8ba1\u7b97\u516c\u5f0f\uff1a\u603b\u8bc4\u8bba\u5206\u6570 \/ \u603b\u8bc4\u8bba\u6761\u6570\u3002<\/p>\n<p><strong>sql \u8bed\u53e5<\/strong><\/p>\n<p>\u7efc\u5408\u4e0a\u8ff0\u8ba1\u7b97\uff0c\u6700\u7ec8\u7684 sql \u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT course_id,                    # \u8bfe\u7a0bid\n       COUNT(1)       AS total_cnt,  # \u8bfe\u7a0b\u8bc4\u8bba\u6761\u6570\n       SUM(score)     AS total_score,# \u8bfe\u7a0b\u8bc4\u8bba\u603b\u5206\n       SUM(t.a_score) AS a_cnt,      # \u8bfe\u7a0b\u597d\u8bc4\u6570\n       SUM(t.b_score) AS b_cnt,      # \u8bfe\u7a0b\u4e2d\u8bc4\u6570\n       SUM(t.c_score) AS c_cnt       # \u8bfe\u7a0b\u5dee\u8bc4\u6570\nFROM (SELECT course_id,\n             score,\n             CASE WHEN score &gt;= 8 THEN 1 ELSE 0 END                        AS a_score,\n             CASE WHEN score &gt;= 8 THEN 0 WHEN score &gt;= 4 THEN 1 ELSE 0 END AS b_score,\n             CASE WHEN score &lt; 4 THEN 1 ELSE 0 END                         AS c_score\n      FROM table_name) t\nGROUP BY course_id;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u7528SQL\u8bed\u53e5\u5bf9\u8bc4\u4ef7\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff0c\u5e76\u8ba1\u7b97\u597d\u8bc4\u7387\u548c\u5747\u5206\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>\u8bc4\u4ef7\u5206\u7ec4\u7edf\u8ba1\u4e4b sql \u5b9e\u8df5 \u5bf9\u4e8e\u590d\u6742\u7684\u8bc4\u4ef7\u6570\u636e\uff0c\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff0c\u4ee5\u63d0\u53d6\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002\u672c\u6587\u65e8\u5728\u901a\u8fc7 sql \u8bed\u53e5\uff0c\u5206\u522b\u7edf\u8ba1\u5404\u7b49\u7ea7\u7684\u6761\u6570\u3001\u603b\u8bc4\u8bba\u6761\u6570\u3001\u597d\u8bc4\u7387\u548c\u5747\u5206\u3002 \u5404\u7b49\u7ea7\u6761\u6570 \u539f\u8868\u5b57\u6bb5\u5df2\u5305\u542b\u5404\u7b49\u7ea7\u6761\u6570\uff0c\u65e0\u9700\u989d\u5916\u8ba1\u7b97\u3002 \u603b\u8bc4\u8bba\u6761\u6570 \u904d\u5386\u8ba1\u7b97\u6240\u6709\u8bc4\u8bba\u6761\u6570\uff0c\u6216\u5355\u72ec\u6267\u884c sql \u8bed\u53e5 select count(1) from table_name\u3002 \u597d\u8bc4\u7387 \u597d\u8bc4\u7387\u8ba1\u7b97\u516c\u5f0f\uff1a\u597d\u8bc4\u6570 \/ \u603b\u8bc4\u8bba\u6761\u6570\u3002 \u5747\u5206 \u5747\u5206\u8ba1\u7b97\u516c\u5f0f\uff1a\u603b\u8bc4\u8bba\u5206\u6570 \/ \u603b\u8bc4\u8bba\u6761\u6570\u3002 sql \u8bed\u53e5 \u7efc\u5408\u4e0a\u8ff0\u8ba1\u7b97\uff0c\u6700\u7ec8\u7684 sql \u8bed\u53e5\u5982\u4e0b\uff1a SELECT course_id, # \u8bfe\u7a0bid COUNT(1) AS total_cnt, # \u8bfe\u7a0b\u8bc4\u8bba\u6761\u6570 SUM(score) AS total_score,# \u8bfe\u7a0b\u8bc4\u8bba\u603b\u5206 SUM(t.a_score) AS a_cnt, # \u8bfe\u7a0b\u597d\u8bc4\u6570 SUM(t.b_score) AS b_cnt, # \u8bfe\u7a0b\u4e2d\u8bc4\u6570 SUM(t.c_score) AS c_cnt [&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-7223","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7223","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=7223"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7223\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}