I’m working with the images table. I need to make sure each player has a primary image if he has more than 1 image. In the following example dataset, this would update either myimage.jpg or hisimage.jpg’s primary to 1 and only1image.jpg’s primary to 1.
Table: Images
Playerid - 16
Image - myimage.jpg
primary - 0
Playerid - 16
Image - hisimage.jpg
primary - 0
Playerid - 17
Image - only1image.jpg
primary - 0
Playerid - 18
Image - jamison.jpg
primary - 1
I’m assuming there must be a Player table? If so, you could use the following…
(This is in MS SQL Server syntax, but the logic should hold in MySQL.)
This deliberately chooses the image with the lowest ID to set as primary. Alter the correlated sub-query to implement different logic.
EDIT
A version that doesn’t use the Player table, but possibly slower (It would need testing)…