MySQL ORDER BY COUNT ()?

I have a table in my database with this structure ( http://cl.ly/3D1D3m1O0v3d0x2j0Z0E )

I have a query through a while loop, and I want to order the prof column for the account.

This is what my request looks like, although I keep getting errors.

$order_list = mysql_query(" SELECT COUNT(prof), FROM prof_rating ORDER BY COUNT(prof) ASC"); 

This is a warning that I keep getting.

Warning: mysql_fetch_assoc () expects parameter 1 to be a resource, boolean is set to

+8
mysql sql-order-by
source share
2 answers

For what it's worth, any use of the aggregate function in the select list means that there will only be one row in the result set. It doesn't make sense to sort the result set with one row.

If you want to get the number of ratings for different prof values, you should use this:

 $order_list = mysql_query(" SELECT prof, COUNT(*) AS PROFCOUNT, FROM prof_rating GROUP BY prof ORDER BY PROFCOUNT ASC'"); 

This will result in multiple rows, one row per value, with the number of rows for each given value.

+12
source share

Highlight the column name and then put it in your order ::

 $order_list = mysql_query(" SELECT COUNT(prof) AS PROFCOUNT, FROM prof_rating ORDER BY PROFCOUNT ASC'"); 
+6
source share

All Articles