MYSQL - generating a subset in a table

I have the following table

Id   Value
1     3
1     12
1     67
2     7
2     99
5     30
5     33
5     4
5     87
5     12
5     1

I want to update it to have this table.

Id  UniqueIdBySubSet    Value
1           1             3
1           2             12
1           3             67
2           1             7
2           2             99
5           1             30
5           2             33
5           3             4
5           4             87
5           5             12
5           6             1

I found the perfect thread on SO, but that was for mssql. I am using mysql 4.1.10.

Another thread can be found here: Sequence generation for subsets of a table .

Does anyone know how I can do this in mysql?

Thanks Jean-Francois

+3
source share
3 answers
SET  @r := 0;
SET  @id := 0;
UPDATE  mytable m
SET     m.UniqueIdBySubSet = IF(@id = id, @r := @r + 1, @r := (@id := id) - id)
ORDER BY
        id, value;
+3
source

Write a quick script to do this. It should take less than 10 lines of code in php.

0
source

, . : , ( insert).

0

Source: https://habr.com/ru/post/1709533/


All Articles