limitphp Posted January 7, 2009 Share Posted January 7, 2009 I have a query that I use only to count the rows: <?php $sqlRowCount = "SELECT COUNT(*) FROM vote, artist, songs WHERE vote.artistID=artist.artistID AND vote.songID=songs.songID AND songs.genre IN ($genres) AND vote.Date > NOW()-INTERVAL $time DAY GROUP BY vote.songID ORDER BY COUNT(vote.songID) DESC"; $resultRowCount = mysql_query($sqlRowCount) or die (mysql_error()); $fetchRowCount = mysql_fetch_row($resultRowCount); $numrows = $fetchRowCount[0]; then I use the same query (which I will eventually put limits on) <?php $queryVote = "SELECT vote.artistID, vote.songID, vote.Date, artist.artistName, artist.artistNameHash, songs.songName, songs.genre, COUNT(vote.songID) FROM vote, artist, songs WHERE vote.artistID=artist.artistID AND vote.songID=songs.songID AND songs.genre IN ($genres) AND vote.Date > NOW()-INTERVAL $time DAY GROUP BY vote.songID ORDER BY COUNT(vote.songID) DESC"; $resultVote = mysql_query($queryVote) or die (mysql_error()); Only the first query returns a lower number than the second query. The first one is returning 4 and the second query returns 7 results. Quote Link to comment Share on other sites More sharing options...
limitphp Posted January 7, 2009 Author Share Posted January 7, 2009 Ok, i took off the last part on the first query: GROUP BY vote.songID ORDER BY COUNT(vote.songID) DESC because I just realized I don't need it. And now the first query is giving me more results than the second one. Quote Link to comment Share on other sites More sharing options...
limitphp Posted January 7, 2009 Author Share Posted January 7, 2009 maybe its not grabbing one of the fields? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 7, 2009 Share Posted January 7, 2009 Those results make sense. With a group by, you get a count for each group. So the first query (with the group by) was returning multiple records. When the group by was removed you were returning one record of the total count. Example Data id | name a | Bob b | Jim a | Dave c | Mary a | Alice c | Rob d | Mike This query SELECT COUNT(*) FROM table GROUP BY id Would return: 3 (for the a records) 1 (for the b records) 2 (for the c records) 1 (for the d records) This query SELECT COUNT(*) FROM table Would return: 7 Quote Link to comment Share on other sites More sharing options...
limitphp Posted January 8, 2009 Author Share Posted January 8, 2009 Those results make sense. With a group by, you get a count for each group. So the first query (with the group by) was returning multiple records. When the group by was removed you were returning one record of the total count. Example Data id | name a | Bob b | Jim a | Dave c | Mary a | Alice c | Rob d | Mike This query SELECT COUNT(*) FROM table GROUP BY id Would return: 3 (for the a records) 1 (for the b records) 2 (for the c records) 1 (for the d records) This query SELECT COUNT(*) FROM table Would return: 7 but now when I removed the group by...the results are higher than the second query.... how can I make it be the same? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.