I’m pulling data form a MySQL database where integer values correlate to more meaningful string outputs. For example, a relation’s attribute “Sport” is defined as integer that can have the value of 1-4. Where 1 => Basketball, 2= Football, 3 = Soccer, 4 = Kickball. Or even a more trivial example, “Employed” where 1=> True, 0=> False. When I pull these integer values out to display them to a user, I need to display the related string value. A co-worker mentioned that he thought there was a MySQL function that would substitute this integer value for the corresponding string. I looked briefly at REPLACE, but do not want to change the value of the stored attribute; just the value of attribute during that SELECT. Is there a SQL function to accomplish this?
My solution to the problem was to use PHP to check the integer value of the returned attribute and replace it accordingly. But, I couldn’t help but wonder what was the best practice for this situation? Is there a distinct computational advantage or disadvantage to either? I assume that if there was swap functionality to SQL query that it would save time versus having to do a conditional check each time I encounter the variable in PHP.
This also led me to ponder if the database should just hold string values? What is the best practice here?