What is the fastest way to count the number of matching records in a MySQL database for each single records?
For example, I have a database of area codes and prefixes of ten thousand calls coming in to my business. I want to count the number of times that each prefix and area code combination occurr.
Right now, I SELECT * that returns the fields AC and PRE
I then run a FOR loop that runs another SELECT * WHERE AC = $AC and PRE = $PRE and counts the number of rows returned.
Then I UPDATE table SET count = ROWS RETURNED FROM STEP 2 WHERE AC = $AC AND PRE = $PRE
So that is three queries for each of ten thousand rows, so it takes a LONG time.
How can I improve that? I simply want to identify all the different combinations and count the database the number of times each unique combination occurs.
Then update a database field with the number of occurrences.