码迷,mamicode.com
首页 > 编程语言 > 详细

MySLQ排序后标记排行

时间:2017-04-26 13:06:53      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:更新   查询   rownum   com   排序   color   技术   分享   表名   

查询排行及所有(表名.*)

1.

set @rownum=0;

SELECT @rownum:=@rownum+1 AS top, customer.*
FROM  customer

 

2.

SELECT @rownum:=@rownum+1 AS top, customer.* 
FROM (SELECT @rownum:=0) r, customer ORDER BY customer.this_month_cost DESC

1和2的结果:

技术分享

查询排行及id(表名.id)

SELECT @rownum:=@rownum+1 AS top, customer.id
FROM (SELECT @rownum:=0) r, customer ORDER BY customer.this_month_cost DESC

结果:

技术分享

排序后更新原表的排行:

UPDATE `customer` c,
 (
    SELECT
        @rownum :=@rownum + 1 AS rownum,
        customer.id
    FROM
        (SELECT @rownum := 0) r,
        customer
    ORDER BY
        customer.this_month_cost DESC
) a
SET c.top = a.rownum
WHERE
    c.id = a.id

 

MySLQ排序后标记排行

标签:更新   查询   rownum   com   排序   color   技术   分享   表名   

原文地址:http://www.cnblogs.com/Alwaysbecoding/p/6767695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!