Mysql Implementation Rownum () after sorting according to the criteria to obtain the rank

Source: Internet
Author: User

Initialize table structure
 DROP TABLE IF EXISTS ' data ';create  TABLE  Span class= "hljs-string" > ' data '  ( ' dates '   varchar  (255 ) character  set  UTF8 default  null , Span class= "hljs-string" > ' id '  int  (11 ) Span class= "Hljs-keyword" >default  null ,  varchar  (255 ) character  set  UTF8 default  null );  INSERT into  ' data ' (' dates ', ' id', ' result ') VALUES (' 2015109101', 1, ' Sheng '); INSERT into ' data ' (' dates ', ' id ', ' result ') VALUES (' 2015110101 ', 2, ' negative '); INSERT into  ' data ' (' dates ', ' id', ' result ') VALUES (' 2015109101 ', 3, ' negative '); INSERT into ' data ' (' dates ', ' id ', ' result ') VALUES (' 2015109101', 4, ' Sheng '); INSERT into  ' data ' (' dates ', ' id', ' result ') VALUES (' 2015110101', 5, ' Sheng '); INSERT into ' data ' (' dates ', ' id ', ' result ') VALUES (' 2015109101 ', 6, ' negative '); INSERT into  ' data ' (' dates ', ' id', ' result ') VALUES (' 2015109101', 7, ' Sheng '); INSERT into ' data ' (' dates ', ' id ', ' result ') VALUES (' 2015110101 ', 8, ' negative ');
Sort
select @rownum:[email protected]+1 AS rownum,id,dates from`data`,(SELECT @rownum:=0) r ORDER BY dates;
Results

Conditional query
SELECT rownum,idfrom    (select @rownum:[email protected]+1 AS rownum,id,dates     from    `data`,(SELECT @rownum:=0) r     ORDER BY dates)b     WHERE id =2;
Results

Written in the last words

Get you a better way to implement RowNum () in MySQL, Welcome to enlighten me.

Mysql Implementation Rownum () after sorting according to the criteria to obtain the rank

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.