1

I have a MySQL 5 database table field media like set('audio','video','photo') What i need to do, is in single SELECT statement prefix it's values with some custom string and space after it, if any of the values are present. For example:

audio,video becomes mediaaudio mediavideo

photo becomes mediaphoto

The specifics of the data does not require an external relationship table to be made for corresponding values, so set is sufficient for the current task. I need to prefix them to uniquely identify them later in search results.

Real example:

id media

1 audio,video

2 audio

3 video

4 photo,video

5

Expected result:

id media

1 mediaaudio mediavideo

2 mediaaudio

3 mediavideo

4 mediaphoto mediavideo

5

5
  • I don't understand. Use CONCAT? Commented Feb 11, 2013 at 15:41
  • 1
    are the values separated by a comma or not? i think if you have an enum field, you can only have one value per column. Commented Feb 11, 2013 at 15:41
  • Oh sorry, my mistake. It's sa SET column Commented Feb 11, 2013 at 15:42
  • Easy with concat / concat with a case statement I think... unless you are storing those comma separated values in a single field? Commented Feb 11, 2013 at 15:42
  • show us sample records (maybe 3 or 4 records) with your desired result. Commented Feb 11, 2013 at 15:42

1 Answer 1

2

Here's one way to do it with INSTR, CONCAT, REPLACE and LENGTH:

SELECT ID, 
  CASE WHEN INSTR(YourField, ',') > 0
    THEN CONCAT('media', REPLACE(YourField, ',', ' media'))
    WHEN LENGTH(YourField) > 0
    THEN CONCAT('media', YourField) 
    ELSE ''
   END media
FROM YourTable

And the Fiddle.

Good luck.

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

Comments

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.