0

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.

5
  • You can get comma-separated string of countries through this: SELECT mv.movie_id, mv.name, GROUP_CONCAT(mvc.country) AS countries FROM movies mv LEFT JOIN movie_countries mvc ON mv.movie_id = mvc.movie_id WHERE 1 GROUP BY mv.movie_id Then you can loop through result and split that string using countries.split(',') Commented Jul 25, 2018 at 18:50
  • In my actual application, it's slightly more complicated, as I have several more tables similar to "movie_countries". Would this still be an efficient solution? I imagine the query would get quite large. Commented Jul 25, 2018 at 18:57
  • 1
    Well, there are many solutions. You can fetch all the results of every table and use lodash or similar libraries for filtering your data. Commented Jul 25, 2018 at 19:01
  • What version of SQL Server are you using? If you're using 2016+ you can have SQL Server format your result set as JSON and return a JSON object Commented Jul 25, 2018 at 19:52
  • I believe it's 2012, that's good to know though! Commented Jul 25, 2018 at 20:14

1 Answer 1

1

Fetch all the data of movies table and movie_countries table in respective variables.

movies.forEach(function(movie) {
    movie.countries = _.remove(movie_countries, function(mc) {
        return movie.movie_id === mc.movie_id; //Edited this from = to ===
    });
});

Sql Way: SELECT mv.movie_id, mv.name, GROUP_CONCAT(mvc.country) AS countries FROM movies mv LEFT JOIN movie_countries mvc ON mv.movie_id = mvc.movie_id WHERE 1 GROUP BY mv.movie_id

movies.forEach(function(movie) {
    movie.countries = movies.countries.length ? movies.countries.split(',') :[];
});
Sign up to request clarification or add additional context in comments.

4 Comments

If my tables are large, won't fetching all the data into variables be very slow?
No, it won't be that slow. I don't know the exact benchmarks but I have been doing similar things on very large tables. First solution, puts more load on JS than on SQL while second solution divides load on both JS and SQL. You can check for yourself which one is fast.
This solution worked for me. Thank you! (For clarification, this solution uses Lodash)
Just a comment, the = in the lodash solution should be ===

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.