https://img2020.cnblogs.com/blog/1101843/202010/1101843-20201029092119794-1182278230.jpg

支付宝

https://img2020.cnblogs.com/blog/1101843/202010/1101843-20201029091740174-1560674849.png

微 信

MySLQ排序后标记排行

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

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

 

-----------------------------------条条大路通罗马------------------------------------------

 

posted @ 2017-04-26 11:27  huangwanlin  阅读(193)  评论(0编辑  收藏  举报
Copyright 2012-2021 林云希科技有限责任公司