I am trying to figure out the most efficient way to do the following using Node.JS and node-mssql. I want an end result that looks like this:
{
movie_id: '1234',
name: 'Hereditary',
countries: [
"Canada",
"United States",
"Australia"
]
},
{
movie_id: '1235',
name: 'Rosemarys Baby',
countries: [
"Canada"
]
}
My tables look like this
movies:
movie_id | name
---------------------
1234 | Hereditary
1235 | Rosemarys Baby
movie_countries:
id | movie_id | country
---------------------
1 | 1234 | Canada
2 | 1234 | United States
3 | 1234 | Australia
4 | 1235 | Canada
So far, I've tried several of the different examples from mssql's Github page. I am able to get the results from the "movies" table, but I'm getting stuck when trying to get the countries for each movie.
movie_id, mv.name, GROUP_CONCAT(mvc.country) AScountriesFROMmoviesmv LEFT JOINmovie_countriesmvc ON mv.movie_id= mvc.movie_idWHERE 1 GROUP BY mv.movie_idThen you can loop through result and split that string using countries.split(',')