limitphp Posted February 25, 2009 Share Posted February 25, 2009 I have a table playlist_songs columns include: id, playlistID, songID I want to select all the playlists that songID '1' is in and count the number of songs in each playlist. Is this possible with 1 query? Right now I'm doing this: SELECT COUNT( id ) FROM playlist_songs WHERE songID = '1' GROUP BY playlistID But its giving me the wrong count for each playlist. Link to comment https://forums.phpfreaks.com/topic/146859-solved-need-help-with-a-count/ Share on other sites More sharing options...
sasa Posted February 25, 2009 Share Posted February 25, 2009 try SELECT a.playlistID, COUNT( b.id ) FROM playlist_songs as a LEFT JOIN playlist_songs as b ON a.playlistID=b.playlistID WHERE a.songID = '1' GROUP BY a.playlistID not tested Link to comment https://forums.phpfreaks.com/topic/146859-solved-need-help-with-a-count/#findComment-771067 Share on other sites More sharing options...
limitphp Posted February 25, 2009 Author Share Posted February 25, 2009 try SELECT a.playlistID, COUNT( b.id ) FROM playlist_songs as a LEFT JOIN playlist_songs as b ON a.playlistID=b.playlistID WHERE a.songID = '1' GROUP BY a.playlistID not tested It worked! thank you..... Thats wierd, left joining the same table with itself. I'm learning more everyday. thanks again. Link to comment https://forums.phpfreaks.com/topic/146859-solved-need-help-with-a-count/#findComment-771072 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.