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
https://forums.phpfreaks.com/topic/200336-mysql-count-function/
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-

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'

Archived

This topic is now archived and is closed to further replies.

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