Reputation: 106
Am kind of new to sql and I have just come with a situation as shown below. the query that i have so far outputs wrong results i.e
$query = "SELECT sch_name, dist_name COUNT(sch_name) AS total_sch FROM school ORDER BY dist_name";
school
**sch_name** **dist_name**
kaoma lusaka
kaloma lusaka
momba mansa
kebwi mansa
matero ndola
EXPECTED OUTPUT
**dist_name** **total_sch**
lusaka 2
mansa 2
ndola 1
Upvotes: 1
Views: 105
Reputation: 125630
You need a GROUP BY
:
SELECT dist_name, COUNT(sch_name) total_sch
FROM school
GROUP BY dist_name
ORDER BY dist_name
Upvotes: 1
Reputation: 6073
Select dist_name,Count(*) total_sch
From School
Group By dist_name
Order By Count(*)
Upvotes: 1