I’m quite a newbie when it comes to MySQL. I’m having a hard time finding this one out, but I bet the solution is quite simple. I have the following tables:
movies
- movie_id
- movie_title
con_genres (connection table between the other 2)
- movie_id
- genre_id
genres
- genre_id
- name
I tried different queries but I keep getting these kind of results
1 | Avatar | Sci-fi
1 | Avatar | Fantasy
2 | Matrix | Sci-fi
2 | Matrix | Fantasy (this one is non existent)
But I want the following results
1 | Avatar | Sci-fi, Fantasy
2 | Matrix | Sci-fi
What’s the best approach to get this through 1 query?
Didn’t run it, but this should work:
The idea here is to join the three tables (which would produce something like your first example result), group on movie id (obviously, since we only want one row per movie) and use
GROUP_CONCATto select all genre names for each movie in one comma-separated string.