Have a table data structure like below:
| id | regid | docid | archived |
|---|---|---|---|
| 1 | 1000 | 1 | 0 |
| 2 | 1000 | 2 | 0 |
| 3 | 1000 | 3 | 0 |
| 4 | 2000 | 1 | 0 |
| 5 | 2000 | 2 | 0 |
| 6 | 3000 | 1 | 0 |
| 7 | 3000 | 2 | 0 |
| 8 | 3000 | 3 | 0 |
| 9 | 3000 | 4 | 0 |
What I'm trying to do update the archived column to 1 where the docid is less than the max docid, by each regid group.
So I should end up with id's 3, 5 & 9 not being set to 1
Have tried:
update table t1
join (select max(docid) as maxdocid, regid from table) t2 on t1.docid < t2.maxdocid and t1.regid = t2.regid
set t1.archived = 1
But doesn't work, only does the first regid group.