1. 程式人生 > >Mysql實現排序

Mysql實現排序

_id 排序 null cas true ble onf log all

排序
 
SELECT
    obj.user_id,obj.score,@rownum := @rownum + 1 AS rownum
FROM
    (
        SELECT
            user_id,
            score
        FROM
            `sql_rank`
        ORDER BY
            score DESC
    ) AS obj,
    (SELECT @rownum := 0) r

有重復

SELECT
    obj.user_id,
    obj.score,
    CASE
WHEN @rowtotal = obj.score THEN
    @rownum
WHEN @rowtotal := obj.score THEN
    @rownum :=@rownum + 1
WHEN @rowtotal = 0 THEN
    @rownum :=@rownum + 1END AS rownum
FROM
    (
        SELECT
            user_id,
            score
        FROM
            `sql_rank`
        ORDER BY
            score DESC
    ) AS obj,
    (SELECT @rownum := 0 ,@rowtotal := NULL) r


C# 運行上面程序時會異常 需要 dbconfig + ";Allow User Variables=True;" 進行處理

Mysql實現排序