Increment column value in mysql based on same ID

Vicky Thakor

This is my table's current status.

id  operation  position
------------------------
1   EDIT          0
1   DELETE        0
2   VIEW          0
3   DELETE        0
3   VIEW          0
3   EDIT          0

I want to update position value in mysql say if for id = 1 1st entry set to 0 second occurrence for same id incremented by 1. So my final output should be like

id  operation  position
------------------------
1   EDIT          0
1   DELETE        1
2   VIEW          0
3   DELETE        0
3   VIEW          1
3   EDIT          2

How can i achive that, any hints ?

Harsh Gupta
set @prev_id = 0;
set @count = 0;

update actions inner join
(select @count := IF(@prev_id = id, @count + 1, 0) as count, @prev_id := id as prev_id, operation
from actions
order by id) as updated
on actions.id = updated.prev_id and actions.operation = updated.operation
set actions.position = updated.count

HTH

Edit: I named the table actions. Also, there was no column to uniquely identify a record, so I used combination of id and operation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL insert, increment column value per same id

From Dev

Mysql column value to use increment id

From Dev

increment if not same value of column

From Dev

MySQL - select row by user_id based on column value of other rows with the same user_id

From Dev

How to increment MySQL Id column value by 1 with powershell

From Dev

MySql increment or not based on a previous value

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

Mysql GROUPing with ORDER based on auto increment column

From Dev

Mysql database table column value increment not working

From Dev

pandas increment count column based on value of another column

From Dev

mysql sum column with same id

From Dev

r - Replace values in a data.frame column with a different value in the same column based unique ID

From Dev

MySQL - JOIN based on value of column?

From Dev

Inserting an increment number that initializes based on another column value

From Dev

Query to increment values based on a value from another column

From Dev

MySQL: Auto-increment based on multi-column PK with transaction

From Dev

MySQL auto increment ID based on the values of two other columns

From Dev

MySQL serial vs auto-increment for id column

From Dev

Atuto increment column does not increses Id numer in mysql table

From Dev

MySQL query to get values based on same column

From Dev

Find next value of AUTO_INCREMENT column in MySQL

From Dev

MySQL auto-increment per distinct value entered in other column?

From Dev

Vectorized calculation of a column's value based on a previous value of the same column?

From Dev

MySQL - Reserving ID's before the initial auto_increment value

From Dev

How to query where same id has different value in specific column Mysql

From Dev

Get column value with same row id

From Dev

Update column with certain value to a column value with same id

From Dev

MySQL Insert data into column based on corresponding ID

From Dev

Increment a value based on return value

Related Related

  1. 1

    MySQL insert, increment column value per same id

  2. 2

    Mysql column value to use increment id

  3. 3

    increment if not same value of column

  4. 4

    MySQL - select row by user_id based on column value of other rows with the same user_id

  5. 5

    How to increment MySQL Id column value by 1 with powershell

  6. 6

    MySql increment or not based on a previous value

  7. 7

    Update value in a column based on another column in the same table in MYSQL

  8. 8

    Mysql GROUPing with ORDER based on auto increment column

  9. 9

    Mysql database table column value increment not working

  10. 10

    pandas increment count column based on value of another column

  11. 11

    mysql sum column with same id

  12. 12

    r - Replace values in a data.frame column with a different value in the same column based unique ID

  13. 13

    MySQL - JOIN based on value of column?

  14. 14

    Inserting an increment number that initializes based on another column value

  15. 15

    Query to increment values based on a value from another column

  16. 16

    MySQL: Auto-increment based on multi-column PK with transaction

  17. 17

    MySQL auto increment ID based on the values of two other columns

  18. 18

    MySQL serial vs auto-increment for id column

  19. 19

    Atuto increment column does not increses Id numer in mysql table

  20. 20

    MySQL query to get values based on same column

  21. 21

    Find next value of AUTO_INCREMENT column in MySQL

  22. 22

    MySQL auto-increment per distinct value entered in other column?

  23. 23

    Vectorized calculation of a column's value based on a previous value of the same column?

  24. 24

    MySQL - Reserving ID's before the initial auto_increment value

  25. 25

    How to query where same id has different value in specific column Mysql

  26. 26

    Get column value with same row id

  27. 27

    Update column with certain value to a column value with same id

  28. 28

    MySQL Insert data into column based on corresponding ID

  29. 29

    Increment a value based on return value

HotTag

Archive