My users submit information for locations. I’m trying to write a query that will tell me the number of submits they’ve done for each location along with that location’s name.
This query seems to be a start in the right direction. It returns the id for each location that the user has submitted information.
SELECT reports.location_id
FROM reports
WHERE reports.user_id =104
ORDER BY reports.locations_id
An example of the return from this query is:
locations_id
99
99
99
112
115
115
For my final html output; however, I would like to show something more along the lines of this:
location_name number_of_submits
name_1 3
name_2 1
name_3 2
Is there a mysql query I could use to get this? Or would I need to use php to iterate through my query’s results (i.e. recognize 99 was returned 3 times and fetch 99’s name from the locations table, then recognize 112 was returned once and fetch its name, and so on).
Thank you…
Use GROUP BY / COUNT: