In an MMORPG server I am refactoring, I have two tables. One for items, and one for spells. Each item has up to 5 spells, so I went with a sparse-matrix format, having 5 columns for spell IDs.
The original devisers of this structure chose to use MyISAM which does not support referencing, causing the items table to contain items with non-existant spell IDs. I wish to find out which items have incorrect spell IDs in order to fix them and perhaps in the long run convert to InnoDB.
So far I've been able to come up with only this:
SELECT COUNT(*)
FROM items
WHERE spellid_1 NOT IN (SELECT entry FROM research.spell)
OR spellid_2 NOT IN (SELECT entry FROM research.spell)
OR spellid_3 NOT IN (SELECT entry FROM research.spell)
OR spellid_4 NOT IN (SELECT entry FROM research.spell)
OR spellid_5 NOT IN (SELECT entry FROM research.spell);
Is there a more elegant way to do so?
EDIT: NULL spellid_n counts as valid since it just means the item doesn't have a spell in that slot.