python - Update a increasing value by MySQL -
i have table in mysql, data this:
+-----------+--------------------------------------+-------+ | address | subnet_id | major | +-----------+--------------------------------------+-------+ | 2.2.2.2 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 0 | | 7.7.7.7 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 0 | | 1.1.1.1 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 0 | | 3.3.3.3 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 0 | | 4.4.4.4 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 0 | | 9.99.9.10 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 0 | +-----------+--------------------------------------+-------+
i need update major
column increasing number (from 0 1, 2, 3...) . example, update subnet_id equals'7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9'. result should this:
+-----------+--------------------------------------+-------+ | address | subnet_id | major | +-----------+--------------------------------------+-------+ | 2.2.2.2 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 0 | | 7.7.7.7 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 1 | | 1.1.1.1 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 2 | | 3.3.3.3 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 3 | | 4.4.4.4 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 4 | | 9.99.9.10 | 7ec1f191-476d-46cd-8fc9-0a8d24dfb8e9 | 5 | +-----------+--------------------------------------+-------+
what should mysql
?
to desired output can use rank query in update using user defined variables, have correct ordering assume every combination of address,subnet_id
unique
update table1 t1 join ( select address,subnet_id, @r:= case when @g = subnet_id @r +1 else 0 end rownum, @g:= subnet_id table1 cross join (select @r:= 0 , @g:=null) vars order subnet_id ) t2 using(address,subnet_id) set major = t2.rownum
Comments
Post a Comment