打赏

mysql 用行号rownum更新顺序号字段

UPDATE customer
SET OrderNo = (
    SELECT
        afterOrder.rowNum
    FROM
        (
            SELECT
                @row_number := CASE
            WHEN @customer_no = s.DepartmentId THEN
                @row_number + 1
            ELSE
                1
            END AS rowNum,
            @customer_no := s.DepartmentId AS stu_DepartmentId,
            s.id
        FROM
            customer s,
            (
                SELECT
                    @row_number := 0,
                    @customer_no := '039c235a-2039-44b0-84b6-bb9586092b78'
            ) t
        ORDER BY
            s.DepartmentId,
            s.CreatedTime
        ) afterOrder
    WHERE
        afterOrder.id = customer.Id
);

mysql 用select后的值 更新表时千万别用这种坑人的写法

UPDATE customer
INNER JOIN (
    SELECT
        @row_number := CASE
    WHEN @customer_no = s.DepartmentId THEN
        @row_number + 1
    ELSE
        1
    END AS rowNum,
    @customer_no := s.DepartmentId AS stu_DepartmentId,
    s.id
FROM
    customer s,
    (
        SELECT
            @row_number := 0,
            @customer_no := '039c235a-2039-44b0-84b6-bb9586092b78'
    ) t
ORDER BY
    s.DepartmentId,
    s.CreatedTime
) afterOrder ON customer.id = afterOrder.id
SET customer.OrderNo = afterOrder.rowNum

 

posted @ 2019-03-19 11:11  KiteRunner1988  阅读(3569)  评论(0编辑  收藏  举报