Jump to content

Recommended Posts

Hi all!

 

I have a problem with a complex query tha involves COUNT() function.

I am writing a script that calculates the number o records for each word in a table, for an example it may look like this

 

Status

 

bad              168

good            7636

excelent        44842

 

I found some code that helped me solve the problem in part, this is:

 

SELECT sed_statistics.status, COUNT(sed_statistics.id) FROM sed_statistics LEFT JOIN equivalences ON sed_statistics.status = equivalences.equiv GROUP BY sed_statistics.status ORDER BY sed_statistics.status ASC LIMIT 0, 30

 

I don't understand completely how this query works, got it from a web page "http://mysql.adoppt.com/blog/frank/mysql-count-the-number-of-records-139",  this works in phpmyadmin, its  results are:

 

STATUS      COUNT(sed_statistics.id) 

bad              168

good            7636

excelent        44842

 

got this code in my script:

 

$query = "SELECT sed_statistics.status, COUNT(sed_statistics.id) FROM sed_statistics LEFT JOIN equivalences ON sed_statistics.status = equivalences.equiv GROUP BY sed_statistics.status ORDER BY sed_statistics.status ASC LIMIT 0, 30";

 

$result = mysql_query($query) or die(mysql_error());

 

// Print out the contents of each row into a table

while($row = mysql_fetch_array($result)){

echo $row['status'];

echo "<br />";

}

 

 

and its results are:

 

 

bad         

good           

excelent

 

don't know how to show the number of records per each row, can somebody point me in the right direction please, i'm stuck with this code, or if you have any other suggestion it will be welcome, any!.

 

Thanks all!.

 

 

Link to comment
https://forums.phpfreaks.com/topic/65759-solved-mysql-count-function-stuck/
Share on other sites

Change your script to:

 

 

$query = "SELECT sed_statistics.status, COUNT(sed_statistics.id) AS my_count FROM sed_statistics LEFT JOIN equivalences ON sed_statistics.status = equivalences.equiv GROUP BY sed_statistics.status ORDER BY sed_statistics.status ASC LIMIT 0, 30";

     

      $result = mysql_query($query) or die(mysql_error());

 

// Print out the contents of each row into a table

      while($row = mysql_fetch_array($result))

    {

      echo $row['status'];

      echo " ";

      echo $row['my_count'];

    }

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.