This is the task at hand: I have a db table named ‘reports’, one of the columns in this table is ‘submitted_by’. This column contains the username of the person who submitted the report.
Now, what I need to do is display a list of usernames ordered by the number of reports they’ve posted. So something like this should come out:
User | Reports
Mark | 25
John | 18
Mary | 7
etc.
I have written this PHP/MySQL code to do this, it’s pretty straightforward but I was wondering if these could be merged into 1 query. Also, would that gain much in terms of efficiency?
$users=array();
$result=mysql_query("select id,submitted_by from reports group by submitted_by");
while($row=mysql_fetch_array($result)){
$result1=mysql_query("select id,submitted_by from reports where submitted_by='$row[submitted_by]' ");
$users[$row[submitted_by]]=mysql_num_rows($result1);
}
arsort($users);
And then just echo each element from $users.
Try the following code: