I have a database full of camp data. Each camp has many campers and has many camp_weeks.
In the table of campers, there is a YAML encoded array of IDs for the camper's camp weeks. The project requires a query for display. Here's a sample row from the campers table:
19770,'Daniela',41,'---
- 406
- 407
')
My current query (based on the excellent tip from this blog):
SELECT
campers.name,
camps.id,
camps.title,
regexp_split_to_array(trim(regexp_replace(campers.camp_weeks, E'[\\n\\r''-]+', ' ', 'g' )), E'\\D+') AS week_array
FROM
campers
LEFT JOIN camps ON campers.camp_id = camps.id
GROUP BY camps.id, campers.id
returns the campers, with a string/array of camp ids like this:
====================================
| Daniela | 41 | sports | 406,407 |
====================================
I would like the query to return
============================================
| Daniela | 41 | sports | cricket, tennis |
============================================
What would be an easy way to do this?
Performance is not a big issue for this query and the simpler the query is the better it will be.
Here's a SQL Fiddle.