Hey all... need a bit of help here... I have a database, and I'll just describe the relevant parts...
The "movie" table has the pk of "movie_id," and has attribute "title"
The "movie_rating" table has no pk, instead having two fk's, one "movie_id" and "user_id," each referencing the movie and user databases. It also has one attribute, "rating."
Now, I must write an SQL query in such a way that it returns the "movie" records with the top 20 highest ratings. There's nowhere in the database that stores the average rating of each movie. Hence the SQL query must look through the rating table and calculate the average for each movie. Then if there are 2 movies with the same rating it takes the movie with more votes as higher.
I was thinking SELECT Max(avg(movie_rating)), WHERE movie_id = whatever?
Also, there's another problem I'm having. I am quite stumped as to how go about doing this. For this same database, there is another table, where "artists" are stored. Then there are 3 associative tables, respectively, act, write, and direct. These all have 2 fk's each, movie_id and artist_id. Now I need to print out all the movies that each artist has starred in, and also print out the average rating (from the tables aforementioned) for each movie.
note: nvm about the PHP displaying part, I'm just sorting out the SQL side first.
I was thinking it'll prolly be something along those lines, but I can't think of where to go from there. Anyone can lend a hand, or a pointing finger?
Thanks in advance for any help!
The "movie" table has the pk of "movie_id," and has attribute "title"
The "movie_rating" table has no pk, instead having two fk's, one "movie_id" and "user_id," each referencing the movie and user databases. It also has one attribute, "rating."
Now, I must write an SQL query in such a way that it returns the "movie" records with the top 20 highest ratings. There's nowhere in the database that stores the average rating of each movie. Hence the SQL query must look through the rating table and calculate the average for each movie. Then if there are 2 movies with the same rating it takes the movie with more votes as higher.
I was thinking SELECT Max(avg(movie_rating)), WHERE movie_id = whatever?
Also, there's another problem I'm having. I am quite stumped as to how go about doing this. For this same database, there is another table, where "artists" are stored. Then there are 3 associative tables, respectively, act, write, and direct. These all have 2 fk's each, movie_id and artist_id. Now I need to print out all the movies that each artist has starred in, and also print out the average rating (from the tables aforementioned) for each movie.
note: nvm about the PHP displaying part, I'm just sorting out the SQL side first.
I was thinking it'll prolly be something along those lines, but I can't think of where to go from there. Anyone can lend a hand, or a pointing finger?
Thanks in advance for any help!