Reputation: 6610
Currently I have an sql query which finds the amount of records in the database for a specific person (Ms White), how can I repeat the function for every surname within the table and print them out in a sensible format?
$query = "SELECT COUNT(Surname) FROM Customers WHERE Surname='White'";
Upvotes: 1
Views: 134
Reputation: 31657
Use
SELECT count(1) as "Counter", surname FROM Customers GROUP BY surname
This will give you output like below.
Counter + Surname
++++++++++++++++++++
10 + SN1
15 + SN2
11 + SN3
Upvotes: 0
Reputation: 43494
Wouldn't it be better to run just one query with the count
per each surname?
select Surname, count(*) as Total from Customers
group by Surname
This will return results like this:
Person Total
White 4
Mustard 2
Plum 1
etc...
Upvotes: 2
Reputation: 455
GROUP BY I think is what you are looking for
SELECT COUNT(*),Surname FROM Customers GROUP BY Surname
Upvotes: 5