MYSQL基础查询语言试题,请大家帮我做一下,谢谢了! 200分!!
1)查询单科分数最高的人。
2)查询两门分数加起来的第2至5名。
3)查询两门总分数在150分以下的人。
4)查询两门平均分数介于60和80的人(不包括60和80)。
5)查询总分大于150分,平均分小于90分的人数。
6)查询总分大于150分,平均分小于90分的人数有几个。
-- 学生成绩表
DROP TABLE if EXISTS grade;
create table `grade` (
`id` double PRIMARY KEY, -- 编号
`name` varchar (300), -- 姓名
`score` double -- 分数
);
insert into `grade` (`id`, `name`, `score`) values('1','n1','59');
insert into `grade` (`id`, `name`, `score`) values('2','n2','66');
insert into `grade` (`id`, `name`, `score`) values('3','n3','78');
insert into `grade` (`id`, `name`, `score`) values('4','n1','48');
insert into `grade` (`id`, `name`, `score`) values('5','n3','85');
insert into `grade` (`id`, `name`, `score`) values('6','n5','51');
insert into `grade` (`id`, `name`, `score`) values('7','n4','98');
insert into `grade` (`id`, `name`, `score`) values('8','n5','53');
insert into `grade` (`id`, `name`, `score`) values('9','n2','67');
insert into `grade` (`id`, `name`, `score`) values('10','n4','88'); 展开
USE test;
DROP TABLE if EXISTS grade;
create table `grade` (
`id` int PRIMARY KEY,
`name` varchar (300),
`score` double
);
insert into `grade` (`id`, `name`, `score`) values('1','n1','59');
insert into `grade` (`id`, `name`, `score`) values('2','n2','66');
insert into `grade` (`id`, `name`, `score`) values('3','n3','78');
insert into `grade` (`id`, `name`, `score`) values('4','n1','48');
insert into `grade` (`id`, `name`, `score`) values('5','n3','85');
insert into `grade` (`id`, `name`, `score`) values('6','n5','51');
insert into `grade` (`id`, `name`, `score`) values('7','n4','98');
insert into `grade` (`id`, `name`, `score`) values('8','n5','53');
insert into `grade` (`id`, `name`, `score`) values('9','n2','67');
insert into `grade` (`id`, `name`, `score`) values('10','n4','88');
首先将上面内容保存在一个名字叫grade.sql 的文件里面,上面我私自修改了id的类型,实在看不下去了。然后登陆数据库使用mysql> source C:/grade.sql;
select name,max(score) from grade;
select name,sum(score) from grade group by name order by score desc limit 1,5;
select name,sum(score) as tot from grade group by name having tot<150;
select name,avg(score) as scavg from grade group by name having scavg < 80 and scavg > 60 ;
select name from grade group by name having sum(score) > 150 and avg(score) < 90;这个应该是查询人吧,6才是查询人数吧
亲测有效
你把代码导入到工具呀?我开始也疑惑,但是你建上面的name下面,有重复的n1,n2等等,所以这应该标明这个人考了几科成绩呀。帮我做做,谢谢了。小弟实在不怎么会查询语句。
直观看就能解题了。对我来说,不需要建具体的表。
但是,数据库有问题,如果按你所说的,比如
insert into `grade` (`id`, `name`, `score`) values('1','n1','59');
insert into `grade` (`id`, `name`, `score`) values('4','n1','48');
insert into `grade` (`id`, `name`, `score`) values('2','n2','66');
insert into `grade` (`id`, `name`, `score`) values('9','n2','67');
怎么才能区分是哪一科的?如果区分不了,单科成绩的查询没办法做。总分和平均分按你说的倒是OK。
select sum(score) from grade group by name limit(2,4)
select sum(score) from grade group by name where sum(score) < 150
select name,avg(sum(score)) from grade group by name where avg(sum(score)) > 60 and avg(sum(score)) < 80
select name,avg(sum(score)) from grade group by name where avg(sum(score)) < 90 and sum(score) > 150
select count(*) from grade group by name where avg(sum(score)) < 90 and sum(score) > 150