views:

39

answers:

2

I have a MySQL table which has three columns:

Authority           | MapID                   | Time
---------------------------------------------------------
animuson@forums     | 000000000000000         | 45.6758
132075829385895     | 000000000000000         | 49.7869
qykumsoy@forums     | 000000000000000         | 44.45
439854390263565     | 000000000000000         | 50.761
user@forums         | 000000000000000         | 44.9
another@auth        | 000000000000000         | 46.123
bingo@nameo         | 000000000000000         | 47.4392

What I'm doing is that once you link your account, it changes the Authority from whatever authoirty name you were using to your account ID number. So if I link 'animuson@forums' and 'qykumsoy@forums' to my account, and my account ID number is '132075829385895' then I would have 3 entries that are duplicated, because the primary key is Authority + Map ID. I'll only ever be changing one authority at a time, but over multiple Map IDs, so I'm trying to figure out a way to just change the Authority to their user ID and if it's a duplicate, select the lowest time and just delete the other one. Is something like this possible with MySQL or do I need to use PHP?

So, after linking both of those accounts, I would end up with this table:

Authority           | MapID                   | Time
---------------------------------------------------------
132075829385895     | 000000000000000         | 44.45
439854390263565     | 000000000000000         | 50.761
user@forums         | 000000000000000         | 44.9
another@auth        | 000000000000000         | 46.123
bingo@nameo         | 000000000000000         | 47.4392

The current MySQL command I'm using which just changes the authority:

UPDATE `tronner_times` SET `auth` = '{$ani->e->user->uid}' WHERE `auth` = '{$actual}'
+1  A: 

That sounds like a job for BEFORE UPDATE trigger.

Mchl
Do you know if it would be easier to select all times where Authority = ID number and then join on all where Authority = New Link where Time < Previous.Time? If you understand what I mean...
animuson
A: 

I would use a stored procedure for the update. In the procedure you can handle the logic for checking other values, delete if necessary and then update.

Palpie