Skip to content
Advertisement

Postgres – Multiple conditions on single column with the WHERE AND condition?

So I have a query as follows:

The last line is most important for this, it gets the average rating of all COMEDY movies.

But overall, I have a couple of tables related to a movie database like movies(title,id),genres(name,id),ratings(movie,rating),hasagenre(movie,genre)…..Ex:

I then want to write a query that gets the average rating of movies that are both COMEDY and ROMANCE, i.e like Get Smart.Thus, I simply updated the last line of query as follows:

However, that doesn’t work and returns 0 rows even though I know for a fact that there are movies that satisfy both genres in the database. What is going wrong here?

What is the proper syntax for an multiple condition AND clause on the same column?

Advertisement

Answer

If you join all the tables to form an average you will multiply the number of rows due to the multiple genres involved. Instead figure out which movies meet the 2 genre condition first, then average the ratings of just those movies.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement