rugzo Posted May 16, 2009 Share Posted May 16, 2009 Hi All, i have a table like below --> id subject ___ ______ 0 pc 1 network 2 account 3 network 4 email 5 pc 6 network ... ... there are over 20000 entries. I want to count each subject and want to list the top ten subjects in one query. Result should look like below --> topten 1- network (3 times) 2- pc (2) 3- account (1) ... Is this possible in one qeury or is there an sql command for this like rank? thanks Quote Link to comment https://forums.phpfreaks.com/topic/158379-solved-top-ten-listing/ Share on other sites More sharing options...
MadTechie Posted May 16, 2009 Share Posted May 16, 2009 Try this SELECT subject, COUNT(subject) AS Occurrences FROM table GROUP BY subject ORDER BY Occurrences DESC LIMIT 0 , 10 change table to table name echo $row['subject']." (".$row['Occurrences']." times)"; EDIT: oops forgot the order (now added) Quote Link to comment https://forums.phpfreaks.com/topic/158379-solved-top-ten-listing/#findComment-835232 Share on other sites More sharing options...
rugzo Posted May 16, 2009 Author Share Posted May 16, 2009 $query = mysql_query("SELECT subject, COUNT(subject) AS Occurrences FROM nsnemailgr GROUP BY subject LIMIT 0 , 10") ; while ($topten = mysql_fetch_arrray($query)){ echo $topten['subject'] ; } i get an error when i do this ---> Fatal error: Call to undefined function mysql_fetch_arrray() in C:\xampp\htdocs\emt.php on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/158379-solved-top-ten-listing/#findComment-835234 Share on other sites More sharing options...
rugzo Posted May 16, 2009 Author Share Posted May 16, 2009 Ok i found the problem i typed arrray wrong Quote Link to comment https://forums.phpfreaks.com/topic/158379-solved-top-ten-listing/#findComment-835237 Share on other sites More sharing options...
MadTechie Posted May 16, 2009 Share Posted May 16, 2009 arrray ? EDIT: ok you found it Quote Link to comment https://forums.phpfreaks.com/topic/158379-solved-top-ten-listing/#findComment-835240 Share on other sites More sharing options...
MadTechie Posted May 16, 2009 Share Posted May 16, 2009 Code should look like this <?php $query = mysql_query("SELECT subject, COUNT(subject) AS Occurrences FROM nsnemailgr GROUP BY subject ORDER BY Occurrences DESC LIMIT 0 , 10") ; while ($topten = mysql_fetch_array($query)){ echo $topten['subject']." (".$topten['Occurrences']." times)<br>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158379-solved-top-ten-listing/#findComment-835242 Share on other sites More sharing options...
rugzo Posted May 16, 2009 Author Share Posted May 16, 2009 Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/158379-solved-top-ten-listing/#findComment-835361 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.