Mysql排名函数

 
-- 排名不跳 


SELECT
    obj.moblie,
    obj.innet_year,
     CASE
WHEN @rowtotal = obj.innet_year THEN
     @rownum
WHEN @rowtotal := obj.innet_year THEN
    @rownum :=@rownum + 1
WHEN @rowtotal = 0 THEN
    @rownum :=@rownum + 1
END AS rownum
   FROM
     (
        SELECT
           moblie,
           innet_year
        FROM
            a5_4g_moblie t  
            ORDER BY t.innet_year DESC
    ) AS obj,
    (SELECT @rownum := 0 ,@rowtotal := NULL) r ;




-- 跳


     SELECT
           moblie,
           innet_year,
           @rownum := @rownum + 1 AS num_tmp,
           @incrnum := CASE
        WHEN @rowtotal = innet_year THEN
            @incrnum
        WHEN @rowtotal := innet_year THEN
            @rownum
        END AS rownum 
        FROM
            a5_4g_moblie t  ,   
            (SELECT @rownum := 0 ,@rowtotal := NULL,@incrnum := 0) r
            ORDER BY t.innet_year DESC

你可能感兴趣的:(mysql)