cezar5122 cezar5122 - 3 years ago 291
MySQL Question

How to merge those columns in mysql or should I use another join?

So I'm tring to get a specifed information from database. I manage to do something like this but I don't know how instead of

weapon, armor, etc
merge it into one column named

SELECT char_name
, obj_Id
, items.item_id
, weapon.name as weapon
, armor.name as armor
, etcitem.name as etc
FROM characters
join items
on characters.obj_Id = items.owner_id
JOIN weapon
ON items.item_id = weapon.item_id
JOIN armor
ON items.item_id = armor.item_id
JOIN etcitem
ON items.item_id = etcitem.item_id;

enter image description here

Answer Source

You can use coalesce():

SELECT char_name, obj_Id, items.item_id,
       coalesce(weapon.name, armor.name, etcitem.name) as item_name
. . .
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download