{"id":153868,"date":"2020-09-10T00:23:08","date_gmt":"2020-09-09T16:23:08","guid":{"rendered":"http:\/\/4563.org\/?p=153868"},"modified":"2020-09-10T00:23:08","modified_gmt":"2020-09-09T16:23:08","slug":"%e5%8a%9b%e6%89%a3-sql-%e9%a2%98%ef%bc%9a-178-%e5%88%86%e6%95%b0%e6%8e%92%e5%90%8d%e7%9a%84%e7%96%91%e9%97%ae%ef%bc%9f%e5%b0%8f%e6%95%b0%e7%82%b9%e5%90%8e%e9%9d%a2%e5%a4%9a%e4%ba%86%e4%b8%80%e4%bd%8d","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=153868","title":{"rendered":"\u529b\u6263 sql \u9898\uff1a 178. \u5206\u6570\u6392\u540d\u7684\u7591\u95ee\uff1f\u5c0f\u6570\u70b9\u540e\u9762\u591a\u4e86\u4e00\u4f4d"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u529b\u6263 sql \u9898\uff1a 178. \u5206\u6570\u6392\u540d\u7684\u7591\u95ee\uff1f\u5c0f\u6570\u70b9\u540e\u9762\u591a\u4e86\u4e00\u4f4d               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>select t.score Score,t.rank 'Rank' from  (     select score, @r:=IF(@sc=score,@r,@r+1) as 'rank', @sc:=score     from Scores, (select @r:=0, @sc:=NULL)init     order by score desc ) t  <\/code><\/pre>\n<p> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/09\/20200911_5f5bbea4d2dbf.png\" alt=\"\u529b\u6263 sql \u9898\uff1a 178. \u5206\u6570\u6392\u540d\u7684\u7591\u95ee\uff1f\u5c0f\u6570\u70b9\u540e\u9762\u591a\u4e86\u4e00\u4f4d\" \/> <\/p>\n<p>\u7ed3\u679c\u90fd\u662f\u5bf9\u7684\uff0c\u4e3a\u5565\u6211\u8fd9\u4e2a\u5c0f\u6570\u70b9\u540e\u9762\u591a\u4e86\u4e00\u4f4d\u5462\uff1f \u597d\u50cf rank \u7684\u6570\u636e\u7c7b\u578b\u4e5f\u4e0d\u5bf9\u5462\u3002\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3228874\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7f51\u5740\u5728\u8fd9\u91cc\uff0c\u5927\u4f6c\u4eec\u5e2e\u5fd9\u7785\u7785<\/p>\n<p>https:\/\/leetcode-cn.com\/problems\/rank-scores\/                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3228875\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : justgodlike1993 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 CAST \u8f6c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3228876\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @justgodlike1993 <br \/>\u4e24\u4e2a\u5b57\u6bb5\u90fd\u5f97\u8f6c\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3228877\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : walkerliu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             select `Score`,CONVERT(SUBSTRING_INDEX(`Rank`,&#8217;-&#8216;,-1),UNSIGNED INTEGER) as `Rank` from ( <br \/>select score, case <br \/> when @preScore &#8211; score &gt; 0.001 then @rank := @rank + 1 <br \/> else @rank <br \/> end <br \/> as `rank` , <br \/> @preScore := score from (select score from Scores order by score desc ) v,(select @rank := 1,@preScore := -1) r <br \/>) p                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3228878\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @walkerliu <br \/>\u611f\u89c9\u91cc\u9762\u4e0d\u9700\u8981\u5d4c\u5957\u4e24\u4e2a\u5b50\u67e5\u8be2\u5427\uff0c\u665a\u4e0a\u6211\u6267\u884c\u4e00\u4e0b\u4f60\u7684\u3002<\/p>\n<p>\u4e3a\u5565\u6211\u90a3\u4e2a\u5916\u5c42\u67e5\u8be2\u5f97\u5230 rank \u7684\u5b57\u6bb5\u770b\u8d77\u6765\u662f\u4e2a\u5b57\u7b26\u4e32\u7684\u7c7b\u578b\u5462\uff0c\u5947\u602a\uff0c\u665a\u4e0a\u770b\u770b                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3228879\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : walkerliu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @amiwrong123 \u6211\u7684\u662f\u82f1\u6587\u7248\u7684\u7f51\u7ad9\uff0c\u8fd9\u4e2a SQL \u662f\u6211 17 \u5e74\u7684\u63d0\u4ea4\uff0c\u73b0\u5728\u7ffb\u51fa\u6765\u5df2\u7ecf accept \u4e0d\u4e86\u4e86\uff0c\u6539\u4e86\u4e00\u4e0b\u624d\u884c(\u52a0\u4e86 &#8220;)\u3002 \u53e6\u5916\u67e5\u8be2\u7ed3\u679c\u7684 score \u5e94\u8be5\u662f\u5bf9\u7684\u4e0d\u5b58\u5728\u5c0f\u6570\u70b9\u7684\u95ee\u9898\u3002Rank \u9700\u8981\u5f3a\u8f6c\u4e00\u4e0b\u6210 int \u7c7b\u578b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3228880\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @walkerliu <br \/>\u597d\u5427\uff0c\u6211\u4e5f\u89c9\u5f97\u5c0f\u6570\u70b9\u5e94\u8be5\u4e0d\u662f\u95ee\u9898\uff0c\u4f46\u4e3a\u6bdb\uff0c\u90a3\u4e2a\u81ea\u5b9a\u4e49\u53d8\u91cf @r \u90fd\u662f\u4e00\u4e2a int \u578b\u7684\uff0c\u4f46\u4ece\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\u518d\u4e00\u67e5\u5c31\u53d8\u6210\u4e86\u4e2a\u5b57\u7b26\u4e32\u4e86\u3002<\/p>\n<p>\u8fd9\u662f\u4e2a\u4ec0\u4e48\u77e5\u8bc6\u70b9\u3002\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u529b\u6263 sql \u9898\uff1a 178. \u5206\u6570&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\/153868"}],"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=153868"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/153868\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153868"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}