I have two tables like so:
books:
id | tags
---+---------------------------------------------------
1 | [philosophy.plato]
2 | [literature.history]
3 | [cultural_history.18th_century.16th_century.history]
tags:
id | name
---+---------------------------------------------------------
1 | literature
2 | history
3 | philosophy
4 | plato
And I am trying to create a join table to cross refer the items in books with their tags. Something like this....
books_tags:
book_id | tag_id
--------+---------------------------------------------------------
1 | 3
1 | 4
2 | 1
2 | 2
How can I take the books.tags string and convert it into an array, look up each item in the array, and insert it into the join table?
So far I have:
SELECT distinct(s.tag)
FROM books t, unnest(string_to_array(trim (t.tags, '[]'), '.')) s(tag);
Which splits in the string into an array, but how do I loop through each item and insert it into the join table?