I have two tables, categories
and movies
.
In movies
table I have a column categories
. That column consists of the categories that movie fits in. The categories are IDs separated by a comma.
Here's an example:
Table categories {
-id- -name-
1 Action
2 Comedy
4 Drama
5 Dance
}
Table movies {
-id- -categories- (and some more columns ofc)
1 2,4
2 1,4
4 3,5
}
Now to the actual question: Is it possible to perform a query that excludes the categories column from the movies table, and instead selects the matching categories from the categories table and returns them in an array? Like a join, but the problem is there are multiple categories separated by comma, is it possible to do some kind of regex?
SELECT movies.*, categories.name FROM movies LEFT JOIN movies_categories ON (movies.id = movies_categories.movie_id) LEFT JOIN categories ON (movies_categories.category_id = categories.id)
– Petrolic