首页 > 数据库 > mysql教程 > Mysql 实现 Rownum() 排序后根据条件获取名次

Mysql 实现 Rownum() 排序后根据条件获取名次

WBOY
发布: 2016-06-07 14:50:55
原创
1499 人浏览过

初始化表结构 DROP TABLE IF EXISTS `data` ; CREATE TABLE `data` ( `dates` varchar ( 255 ) CHARACTER SET utf8 DEFAULT NULL , `id` int ( 11 ) DEFAULT NULL , `result` varchar ( 255 ) CHARACTER SET utf8 DEFAULT NULL ); INSERT INTO `data` ( `dat

初始化表结构

<code class="language-sql hljs "><span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-keyword">IF</span> <span class="hljs-keyword">EXISTS</span> <span class="hljs-string">`data`</span>;</span>
<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">`data`</span> (
  <span class="hljs-string">`dates`</span> <span class="hljs-keyword">varchar</span>(<span class="hljs-number">255</span>) <span class="hljs-keyword">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8 <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">NULL</span>,
  <span class="hljs-string">`id`</span> <span class="hljs-keyword">int</span>(<span class="hljs-number">11</span>) <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">NULL</span>,
  <span class="hljs-string">`result`</span> <span class="hljs-keyword">varchar</span>(<span class="hljs-number">255</span>) <span class="hljs-keyword">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8 <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">NULL</span>
);</span>
<span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-string">`data`</span> (<span class="hljs-string">`dates`</span>, <span class="hljs-string">`id`</span>, <span class="hljs-string">`result`</span>) <span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'2015109101'</span>, <span class="hljs-number">1</span>, <span class="hljs-string">'胜'</span>);</span>
<span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-string">`data`</span> (<span class="hljs-string">`dates`</span>, <span class="hljs-string">`id`</span>, <span class="hljs-string">`result`</span>) <span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'2015110101'</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'负'</span>);</span>
<span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-string">`data`</span> (<span class="hljs-string">`dates`</span>, <span class="hljs-string">`id`</span>, <span class="hljs-string">`result`</span>) <span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'2015109101'</span>, <span class="hljs-number">3</span>, <span class="hljs-string">'负'</span>);</span>
<span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-string">`data`</span> (<span class="hljs-string">`dates`</span>, <span class="hljs-string">`id`</span>, <span class="hljs-string">`result`</span>) <span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'2015109101'</span>, <span class="hljs-number">4</span>, <span class="hljs-string">'胜'</span>);</span>
<span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-string">`data`</span> (<span class="hljs-string">`dates`</span>, <span class="hljs-string">`id`</span>, <span class="hljs-string">`result`</span>) <span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'2015110101'</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'胜'</span>);</span>
<span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-string">`data`</span> (<span class="hljs-string">`dates`</span>, <span class="hljs-string">`id`</span>, <span class="hljs-string">`result`</span>) <span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'2015109101'</span>, <span class="hljs-number">6</span>, <span class="hljs-string">'负'</span>);</span>
<span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-string">`data`</span> (<span class="hljs-string">`dates`</span>, <span class="hljs-string">`id`</span>, <span class="hljs-string">`result`</span>) <span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'2015109101'</span>, <span class="hljs-number">7</span>, <span class="hljs-string">'胜'</span>);</span>
<span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> <span class="hljs-string">`data`</span> (<span class="hljs-string">`dates`</span>, <span class="hljs-string">`id`</span>, <span class="hljs-string">`result`</span>) <span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'2015110101'</span>, <span class="hljs-number">8</span>, <span class="hljs-string">'负'</span>);</span></code>
登录后复制

排序

<code class="language-sql hljs "><span class="hljs-operator"><span class="hljs-keyword">select</span> @rownum:=@rownum+<span class="hljs-number">1</span> <span class="hljs-keyword">AS</span> rownum,id,dates 
<span class="hljs-keyword">from</span>
<span class="hljs-string">`data`</span>,(<span class="hljs-keyword">SELECT</span> @rownum:=<span class="hljs-number">0</span>) r 
<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> dates;</span></code>
登录后复制

结果

这里写图片描述

条件查询

<code class="language-sql hljs "><span class="hljs-operator"><span class="hljs-keyword">SELECT</span> rownum,id
<span class="hljs-keyword">from</span>
    (<span class="hljs-keyword">select</span> @rownum:=@rownum+<span class="hljs-number">1</span> <span class="hljs-keyword">AS</span> rownum,id,dates
     <span class="hljs-keyword">from</span>
    <span class="hljs-string">`data`</span>,(<span class="hljs-keyword">SELECT</span> @rownum:=<span class="hljs-number">0</span>) r 
    <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> dates)b 
    <span class="hljs-keyword">WHERE</span> id =<span class="hljs-number">2</span>;</span></code>
登录后复制

结果

这里写图片描述

写在最后的话

获取你有更好的方法在mysql中来实现Rownum(),欢迎不吝赐教。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板