select sum(人数) as cnt from your_table group by subStr(班级,1,1)
比如你现在的表是: 班级 人数 年级人数 1000 20 50 1001 30 50 2000 10 30 2001 20 30select sum(人数) from table group by substr(班级,1,2) 取班级的前2位
那么已经给出SQL了阿,具体怎样要自己调了
select t.*,v.c_nj from ( select 班級,count(1) from tb group by 班級) t, (select substr(班級,1,1) nj,count(1) c_nj from tb group by substr(班級,1,1) )v where substr(t.班級,1,1)=v.nj
from your_table
group by subStr(班级,1,1)
班级 人数 年级人数
1000 20 50
1001 30 50
2000 10 30
2001 20 30select sum(人数) from table group by substr(班级,1,2)
取班级的前2位
from
(
select 班級,count(1) from tb
group by 班級) t,
(select substr(班級,1,1) nj,count(1) c_nj from tb
group by substr(班級,1,1)
)v
where substr(t.班級,1,1)=v.nj