MySQL implementation row_number () function

Source: Internet
Author: User
Query test table by group_id group SORT_ID Top 100

SELECT id AS ‘原数据ID‘,group_id AS ‘分组ID‘,sort_id AS ‘排序条件‘,num AS ‘排名‘FROM     (SELECT  @row_number := CASE WHEN  @customer_no = group_id  THEN  @row_number + 1  ELSE  1 END   AS  num /*如果同group_id那么排名加1,否则更新为1*/    ,@customer_no := group_id  AS  group_id /*重新加载group_id*/    ,id , sort_id /*原有数据*/ FROM  test,( SELECT  @customer_no := 0,@row_number := 0)  AS  t ORDER BY group_id ,sort_id DESC ,id)a WHERE num<=100

MySQL implementation row_number () function

Related Article

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.