Jump to content

[SOLVED] Need Help With a Count


limitphp

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.