{"id":158644,"date":"2020-09-11T23:04:41","date_gmt":"2020-09-11T15:04:41","guid":{"rendered":"http:\/\/4563.org\/?p=158644"},"modified":"2020-09-11T23:04:41","modified_gmt":"2020-09-11T15:04:41","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%aa-sql-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=158644","title":{"rendered":"\u8bf7\u6559\u4e00\u4e2a SQL \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e00\u4e2a SQL \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sockball07 <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u73b0\u6709 2 \u5f20\u8868<\/p>\n<pre><code>course     course_id  int   \u8bfe\u7a0b ID     score      float \u8bfe\u7a0b\u5f97\u5206\uff0c\u6ee1\u5206 100,60 \u53ca\u683c     student_id int   \u5b66\u751f ID  student     student_id int   \u5b66\u751f ID     name       int   \u5b66\u751f\u59d3\u540d <\/code><\/pre>\n<p>\u4e00\u4e2a\u5b66\u751f\u53ef\u80fd\u6709\u591a\u4e2a\u8bfe\u7a0b\u5f97\u5206\uff08\u5373 student \u4e00\u5bf9\u591a\u4e8e course \uff09<\/p>\n<p>\u73b0\u5b9a\u4e49\u4e00\u4e2a\u5b66\u751f\u53ca\u683c\u7684\u6761\u4ef6\u4e3a\u6240\u6709\u8bfe\u7a0b\u4e2d\u53ea\u8981\u6709\u4e00\u95e8\u8bfe\u7a0b\u53ca\u683c\u5219\u7b97\u53ca\u683c\uff0c\u53ea\u6709\u6240\u6709\u8bfe\u7a0b\u4e0d\u53ca\u683c\u7684\u60c5\u51b5\u4e0b\u624d\u7b97\u4e0d\u53ca\u683c<\/p>\n<p>\u6c42\u6240\u6709\u53ca\u683c\u7684\u5b66\u751f\u548c\u6240\u6709\u4e0d\u53ca\u683c\u7684\u5b66\u751f\u3002<\/p>\n<p>\u53ca\u683c\u5f88\u7b80\u5355<\/p>\n<pre><code>SELECT     s.student_id,     s.name FROM     student s INNER JOIN     course c ON     s.student_id = c.student_id WHERE     c.score &gt;= 60 GROUP BY     student_id <\/code><\/pre>\n<p>\u4f46\u662f\u4e0d\u53ca\u683c\u5374\u4e0d\u80fd\u76f4\u63a5\u4ee5 <code>WHERE c.score &lt; 60 GROUP BY student_id<\/code> \u5f97\u51fa\u7ed3\u679c<\/p>\n<p>\u95ee\u9664\u4e86\u4f7f\u7528 \uff08 NOT IN \u53ca\u683c\u7684\u5b66\u751f\uff09\u7684\u7ed3\u679c \u8fd8\u6709\u4ec0\u4e48\u6bd4\u8f83\u597d\u7684\u65b9\u6cd5\uff1f<\/p>\n<p>\u6b64\u5904\u53ea\u662f\u6a21\u62df\u771f\u5b9e\u573a\u666f\uff0c\u5b9e\u9645\u4e0a\u7684\u6761\u4ef6\u6bd4<code>score &gt;= 60<\/code>\u8981\u591a\uff0c\u6240\u4ee5\u4e0d\u5e94\u8be5\u7279\u522b\u9488\u5bf9\u6b64\u5904\u4f7f\u7528<code>MIN<\/code>\u6765\u505a&#8230;\uff08\u5982\u679c\u56de\u590d\u6709\u6211\u4f1a\u518d\u8bf4\u660e\u7684\uff09<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3396090\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wisunny <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             exists                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3396091\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : TimePPT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u60f3\u4e86\u4e0b\uff0c\u8bd5\u8bd5\u7528 case when then else \u628a\u53ca\u683c\u4e0d\u53ca\u683c\u4e8c\u503c\u5316\uff08\u53ca\u683c\u8d4b\u503c 1\uff0c\u4e0d\u53ca\u683c 0 \uff09\uff0c\u7136\u540e group by \u6c42\u548c\uff0c\u548c\u5927\u4e8e 0 \u7684\u53ca\u683c\uff0c\u7b49\u4e8e 0 \u7684\u4e0d\u53ca\u683c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3396092\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chotow <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             SELECT s.name, c.score, c.course_id<br \/>FROM course AS c<br \/> JOIN student AS s ON s.student_id = c.student_id<br \/> LEFT JOIN course AS c_tmp ON c_tmp.student_id = c.student_id AND c.score &lt; c_tmp.score<br \/>WHERE c_tmp.course_id IS NULL<br \/> AND c.score &lt; 60<\/p>\n<p>https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/example-maximum-column-group-row.html                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3396093\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wspsxing <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             select student_id, name, pass, cc from (select student_id, sum(score &gt;= 60) as pass, count(score) as cc from course group by student_id)a join student as s on a.student_id = s.id where pass &lt;= 0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3396094\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : awen233333 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             having max(c.score)&lt;60                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e2a SQL \u95ee\u9898 \u8cc7\u6df1\u5927\u4f6c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/158644"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=158644"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/158644\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158644"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}