4

I'm using view to do most of my reports for data on MySQL. In one of the table, I would like to treat a particular column like ENUM when selecting data, but it's currently int.

I knew it's possible to use inner join to accomplish this, but I'll keep that as my last option.

Is there a way to build the view or select query so that i can sort of cast int into enum and have string output instead of int? Something like

SELECT CONVERT(int_column, ENUM('A', 'B', 'C')) FROM table;

I need to store that column as int, changing the column to enum will require too many changes at the application level, and this is only needed for one particular report, so should be fine. The enum will have 11 values only.

If casting is not possible, then can tertiary operator or inline if works in select?

Thanks

1 Answer 1

10

Use the ELT() Function

   SELECT ELT(int_column, 'A', 'B', 'C') FROM table;

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_elt

Sign up to request clarification or add additional context in comments.

1 Comment

Note that for zero-indexed columns, you can just do SELECT ELT(int_column + 1, 'A', 'B', 'C') FROM table;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.