create table [tb]([name] varchar(3),[class] varchar(4)) insert [tb] select 'abc','一班' union all select 'acc','一班' union all select 'add','一班' union all select 'bcc','二班' union all select 'bbc','二班'select class from tb group by class --等价于 select distinct class from tb /** class ----- 二班 一班 **/--按班统计人数 select class,count(1)人数 from tb group by class /** class 人数 ----- ----------- 二班 2 一班 3 **/
insert [tb]
select 'abc','一班' union all
select 'acc','一班' union all
select 'add','一班' union all
select 'bcc','二班' union all
select 'bbc','二班'select class from tb group by class
--等价于
select distinct class from tb
/**
class
-----
二班
一班
**/--按班统计人数
select class,count(1)人数 from tb group by class
/**
class 人数
----- -----------
二班 2
一班 3
**/