如何在MySQL的新列中将ID拆分为多个值

时间:2019-02-15 08:40:34

标签: mysql

我的表中有多个ID,我必须附加值00,01,02 ... 99

id                   newid
1768053             176805300
1768053             176805301
1768053             176805302
1768053             176805303
.                   .
.                   .
.                   .
.                   .
1768053             176805399

我要上面在mysql中显示的newid。请尽快给我。

1 个答案:

答案 0 :(得分:0)

假设MySQL 5.x,您需要使用用户变量通过以下方式在分区上模拟row_number:

SELECT id, id * 100 + rn AS newid
FROM (
    SELECT id, @crn := CASE
        WHEN @cid = id THEN @crn + 1
        WHEN @cid := id THEN 1
    END AS rn
    FROM t, (SELECT @cid := NULL, @crn := NULL) AS x
    ORDER BY id
) AS X
ORDER BY id, newid