I have the following query:
SELECT CASE WHEN `def_spell`.`type` = 0 THEN `spells_damage`.*
WHEN `def_spell`.`type` = 1 THEN `spells_weaken`.*
END
FROM `def_spell`
LEFT JOIN `spells_damage` ON `def_spell`.`id` = `spells_damage`.`spell_id`
LEFT JOIN `spells_weaken` ON `def_spell`.`id` = `spells_weaken`.`spell_id`
WHERE `def_spell`.`id` = 1;
Hopefully that makes sense... I'm basically trying to select everything from spells_damage providing type is 0 and everything from spells_weaken providing type is 1.
I'd also like to select everything from def_spell regardless.
Can someone please fix the query? I've never used cases like this before and not sure how to.
def_spell.type= 0 THENspells_damage.* WHENdef_spell.type= 1spells_weaken.* END FROMdef_spellLEFT JOINspells_damageONdef_spell.id=spells_damage.spell_idLEFT JOINspells_weakenONdef_spell.id=spells_weaken.spell_idWHEREdef_spell.id= 1;SELECT CASE WHEN ... table1.* ELSE table2.*works (it *might if the tables have the same exact number of columns and atatypes. If it does, from a maintenance view it's terrible, and if you worked in my shop it wouldn't fly (neither doesSELECT *). There has to be a better way, even if it means writing a longer SQL statement, to make this more readable and maintainable.