logged_with_bugmenot Posted December 12, 2006 Share Posted December 12, 2006 Hello everybodyI have a table for movie names and one below it for movie comments with the movie name database having a primary key of movieidx.I'm trying to return the movies ordered by those with the highest numbers of comments in the database. The query I'm running is:SELECT movMovies.moviename,movMovies.movieidx,COUNT(movData.movieidx) AS cnt FROM movMovies,movData WHERE movMovies.movieidx=movData.movieidx GROUP BY movMovies.movieidx ORDER BY cnt DESC(movMovies holds the movie names and movData holds the comments) It works fine, however if a movie has no comments (there's a movieidx in movMovies but no records with the corresponding movieidx in movData), it doesn't get returned at all. I think it has something to do with the WHERE clause as it can't be true if there's no corresponding movData record in there, but I'm not quite sure how to proceed.Your help would be appreciated! Link to comment https://forums.phpfreaks.com/topic/30399-help-with-a-group-by-query/ Share on other sites More sharing options...
artacus Posted December 13, 2006 Share Posted December 13, 2006 No, you need a LEFT JOIN for the comments. Link to comment https://forums.phpfreaks.com/topic/30399-help-with-a-group-by-query/#findComment-140063 Share on other sites More sharing options...
fenway Posted December 15, 2006 Share Posted December 15, 2006 So:[code]SELECT movMovies.moviename,movMovies.movieidx,COUNT(movData.movieidx) AS cnt FROM movMovies LEFT JOIN movData ON movMovies.movieidx=movData.movieidx GROUP BY movMovies.movieidx ORDER BY cnt DESC[/code] Link to comment https://forums.phpfreaks.com/topic/30399-help-with-a-group-by-query/#findComment-142102 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.