I have the following table structure:
status: TINYINT
locked_by: INTEGER
date: DATETIME
page: TINYINT
my_id: BIGINTEGER
PRIMARY KEY IS (page, my_id)
and have the following indexes:
INDEX idx_col_lock_status (locked_by, status),
INDEX idx_my_id (my_id)
At some point we have the following rows:
status, locked_by, date, page, my_id
2 243 NULL 0 1
0 0 NULL 1 1
1 244 NULL 2 1
2 255 NULL 0 2
2 2556 NULL 1 2
2 255 NULL 2 2
I would like to update all rows (set status = 0, locked_by = )
that have their status value != 2 or
they have status value = 2 *but* there is
another row with the same my_id that has status != 2
After the above update the above rows should become.
status, locked_by, date, page, my_id
0 0 NULL 0 1 --note this line
0 0 NULL 1 1
0 0 NULL 2 1 --and this line
2 255 NULL 0 2
2 2556 NULL 1 2
2 255 NULL 2 2
I am using Mysql version 5.1.63.
Thanks
That should do it:
You have to work with a temporary table here, because you can’t use the same table for update and a subquery.