mr cracker Posted May 1, 2010 Share Posted May 1, 2010 Hello Guys! I have the following table: 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 More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 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 https://forums.phpfreaks.com/topic/200336-mysql-count-function/#findComment-1051339 Share on other sites More sharing options...
mr cracker Posted May 1, 2010 Author Share Posted May 1, 2010 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 https://forums.phpfreaks.com/topic/200336-mysql-count-function/#findComment-1051419 Share on other sites More sharing options...
Psycho Posted May 1, 2010 Share Posted May 1, 2010 You need to remove the extra 'as', plus add DESC to order from high to low SELECT concepto, COUNT(concepto) As conceptocount FROM tbl_ventas GROUP BY concepto ORDER BY conceptocount DESC Link to comment https://forums.phpfreaks.com/topic/200336-mysql-count-function/#findComment-1051420 Share on other sites More sharing options...
mr cracker Posted May 1, 2010 Author Share Posted May 1, 2010 Thank you both, now its working correctly!!! Link to comment https://forums.phpfreaks.com/topic/200336-mysql-count-function/#findComment-1051659 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.