mysql——单表查询——聚合函数——示例

create table score ( xh int(50),
                     km varchar(50),
                     cj int(50)
                   );

select * from score;

insert into score values(1,'shuxue',80);
insert into score values(1,'yuwen',70);
insert into score values(1,'yingyu',40);

insert into score values(2,'shuxue',40);
insert into score values(2,'yuwen',60);
insert into score values(2,'yingyu',50);

insert into score values(3,'shuxue',60);
insert into score values(3,'yuwen',20);
insert into score values(3,'yingyu',90);

insert into score values(4,'shuxue',50);
insert into score values(4,'yuwen',60);
insert into score values(4,'yingyu',70);

select xh,sum(cj) from score where xh = 1;   查询此同学的总成绩;
select xh,sum(cj) from score where xh = 4;

select xh,sum(cj) from score group by xh;     查询每一个同学的各科总和成绩;

select km,max(cj) from score group by km;      查询各个科目的最高成绩;

select km,avg(cj) from score group by km;   查询每一科目的平均成绩;

select km,max(cj) from score group by km;    查询每一科目的最高成绩;

select km,min(cj) from score group by km;    查询每一科目的最低成绩;

猜你喜欢

转载自www.cnblogs.com/xiaobaibailongma/p/12092947.html