Jump to content

MySQL COUNT Function


mr cracker

Recommended Posts

Hello Guys!  :)

 

I have the following table:

tabla.gif

 

Using this code:

$query = "SELECT concepto, COUNT(concepto) FROM tbl_ventas GROUP BY concepto "; 

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

// Print out result
while($row = mysql_fetch_array($result)){
echo "There are ". $row['COUNT(concepto)'] ." ". $row['concepto'] ." items.";
echo "<br />";
}


 

 

I get this result:

There are 3 Coca Cola 600ml items.

There are 5 Leche items.

There are 2 Sprite 600ml items.

 

But they are ordered alphabeticaly and i want them to be ordered numericaly in a DESCENDIG order like this:

There are 5 Leche items.

There are 3 Coca Cola 600ml items.

There are 2 Sprite 600ml items.

 

i know i have to use ORDER BY , and i´ve tried it but with no luck.

Thanks.

Link to comment
Share on other sites

Try this:

 

$query = "SELECT concepto, COUNT(concepto) As conceptocount As FROM tbl_ventas GROUP BY concepto ORDER BY conceptocount";

 

you would have to try this also:

   echo "There are ". $row['conceptocount'] ." ". $row['concepto'] ." items.";

 

-cb-

Link to comment
Share on other sites

Thanks for your reply.

 

I changed the code to look like this:

 

$query = "SELECT concepto, COUNT(concepto) As conceptocount As FROM tbl_ventas GROUP BY concepto ORDER BY conceptocount";

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

// Print out result
while($row = mysql_fetch_array($result)){
   echo "There are ". $row['conceptocount'] ." ". $row['concepto'] ." items.";

echo "<br />";
}

 

 

but i get this error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'As FROM tbl_ventas GROUP BY concepto ORDER BY conceptocount'

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.