I have a table of ‘games’ with a different entry for each game like so:
gameID internalName
1 game1
2 game 1
3 game 2
4 game2
5 game3
etc.
So, the problem is now that I want to combine cases where the game’s titles (with whitespace stripped) are identical. In other the row with gameID of 2 would be merged with 1, and removed.
In addition, I want to update any entries in other tables that are using the gameID as a foreign key, to point to the new key.
I know I can do something like so, to get a list of which titles have duplicates:
SELECT gameID, count(REPLACE(internalName, ' ', '')), internalName FROM games GROUP BY REPLACE(internalName, ' ', '') HAVING count(REPLACE(internalName, ' ', '')) > 1
However, that doesn’t really help me to achieve what I need to do.
Any help?
something like that could help you to start
but if you have 3 items to merge, this won’t work that well…
see http://sqlfiddle.com/#!2/fb294/6
but within a stored procedure or in php (or other language), you should be able to get what you want with that.
BETTER
with this one, even 3, or 4, or 5 identical result will work
http://sqlfiddle.com/#!2/8fb21/1
EDIT : an example of (untested) stored procedure