如何实现多重匹配的筛选,同时统计总数
背景:
数据表userinfo,内有主要字段userName,school,grade,即用户名和其所在的学校名和年级;年级包括初一,初二,直到大四。但各所学校的类型未知。
欲通过年级来判别学校类型,统计所有初中学校各自的注册人数。
查询过程中,初一,初二,初三这三个字符的任一个出现,即可记该用户为初中用户,同时判断其所在学校为初中。
问题如下:
sql = "select top 10000 school, count(school) as counts from userinfo " +
" group by school order by counts desc ";
上述语句实现所有学校的注册人数统计;
该如何修改语句,才能完成单独对中初中学校的筛选统计呢?
------解决方案--------------------select school, count(school) as counts from userinfo
where grade like '初%'
group by school order by counts desc
------解决方案--------------------select school, count(school) as counts from userinfo
where grade like '%初一%' OR grade like '%初二%' OR grade like '%初三%'
group by school order by counts desc
你这表设计也....肯定年级有个字段来表示好点呐.
------解决方案--------------------select school, count(*) as counts
from userinfo (nolock)
where grade like '%初%'
group by school
order by counts desc