I’m attempting to construct an SQL statement for MySQL that will update a single row in a group. Taking the records below I need to update the IsPrimary flag so that the earliest record for each user is set to 1.
ID | UserID | Inserted | IsPrimary
----------|-------------|-------------|----------
000f83 | 79b8c3 | 2012-03-14 | 0
001401 | 79b8c3 | 2012-03-15 | 0
002e7d | 4652a2 | 2012-02-22 | 0
003ca6 | 4652a2 | 2012-02-13 | 0
So the above records would end up as:
ID | UserID | Inserted | IsPrimary
----------|-------------|-------------|----------
000f83 | 79b8c3 | 2012-03-14 | 1
001401 | 79b8c3 | 2012-03-15 | 0
002e7d | 4652a2 | 2012-02-22 | 0
003ca6 | 4652a2 | 2012-02-13 | 1
Give this a ry:
Here is the fiddle.