I’m attempting to set a row value to 1 when it is the only row in a group, with a single query.
Current Table (CATEGORY_BRAND):
|categoryId | brandId | featured |
|-----------+---------+----------|
| 1 | 100 | -1 |
| 1 | 101 | -1 |
| 1 | 102 | -1 |
| 2 | 100 | -1 |
| 3 | 110 | -1 |
| 3 | 111 | -1 |
| 4 | 102 | -1 |
| 5 | 101 | -1 |
| 6 | 100 | -1 |
| 6 | 110 | -1 |
| ... | ... | ... |
Hoped for Result (CATEGORY_BRAND):
|categoryId | brandId | featured |
|-----------+---------+----------|
| 1 | 100 | -1 |
| 1 | 101 | -1 |
| 1 | 102 | -1 |
| 2 | 100 | 1 |
| 3 | 110 | -1 |
| 3 | 111 | -1 |
| 4 | 102 | 1 |
| 5 | 101 | 1 |
| 6 | 100 | -1 |
| 6 | 110 | -1 |
| ... | ... | ... |
(My sincerest apologies if this is a duplicate, I couldn’t find anyone else doing an UPDATE like this.)
This may not be the most performant, but should work (although you should test it first).