SQL COUNT函数查询为相同的列值

时间:2014-08-14 07:14:16

标签: sql count

对sql有点新意,我刚刚遇到如下所示的情况。 到目前为止我的查询输出了错误的结果,即

$ query =" SELECT sch_name,dist_name COUNT(sch_name)AS total_sch FROM school ORDER BY dist_name&#34 ;;

学校

  **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

2 个答案:

答案 0 :(得分:1)

您需要GROUP BY

SELECT dist_name, COUNT(sch_name) total_sch
FROM school
GROUP BY dist_name
ORDER BY dist_name

答案 1 :(得分:1)

Select dist_name,Count(*) total_sch
From School
Group By dist_name
Order By Count(*)