Jump to content

[SOLVED] top ten listing


rugzo

Recommended Posts

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

$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

 

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.