mysql实现SQL统计的实例

黄舟
Lepaskan: 2017-09-08 13:29:40
asal
1805 orang telah melayarinya

建表语句

/*Table structure for table `stuscore` */

DROP TABLE IF EXISTS `stuscore`;

CREATE TABLE `stuscore` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `subject` varchar(20) DEFAULT NULL,
  `score` varchar(20) DEFAULT NULL,
  `stuid` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

/*Data for the table `stuscore` */

insert  into `stuscore`(`id`,`name`,`subject`,`score`,`stuid`) values 
(1,'张三','数学','89','1'),
(2,'张三','语文','80','1'),
(3,'张三','英语','70','1'),
(4,'李四','数学','90','2'),
(5,'李四','语文','70','2'),
(6,'李四','英语','80','2'),
(7,'王五','数学','55','3'),
(8,'王五','语文','92','3'),
(9,'王五','英语','74','3'),
(10,'赵六','数学','62','4'),
(11,'赵六','语文','81','4'),
(12,'赵六','英语','93','4');

建表语句
Salin selepas log masuk

问题:

1. 计算每个人的总成绩并排名(要求显示字段:姓名,总成绩)

答案


1 SELECT a.name, SUM(score) sum_score FROM stuscore a
2 GROUP BY a.name ORDER BY sum_score DESC
Salin selepas log masuk

2. 计算每个人的总成绩并排名(要求显示字段: 学号,姓名,总成绩)

答案


1 SELECT a.stuid, a.name, SUM(score) sum_score FROM stuscore a
2 GROUP BY a.name ORDER BY sum_score DESC
Salin selepas log masuk

3. 计算每个人单科的最高成绩(要求显示字段: 学号,姓名,课程,最高成绩)

答案


1 SELECT a.stuid, a.name, a.subject, a.score FROM stuscore a
2 JOIN (
3     SELECT stuid, MAX(score) max_score FROM stuscore GROUP BY stuid4 )b ON a.stuid=b.stuid5 WHERE a.score=b.max_score
Salin selepas log masuk

4. 计算每个人的平均成绩(要求显示字段: 学号,姓名,平均成绩)

答案


1 SELECT DISTINCT a.stuid, a.name, b.avg_score FROM stuscore a
2 JOIN (
3     SELECT stuid, AVG(score) avg_score FROM stuscore GROUP BY stuid
4 )b ON a.stuid=b.stuid
Salin selepas log masuk

5. 列出各门课程成绩最好的学生(要求显示字段: 学号,姓名,科目,成绩)

答案


1 SELECT DISTINCT a.stuid, a.name, a.subject, a.score FROM stuscore a
2 JOIN (
3     SELECT subject, MAX(score) max_score FROM stuscore GROUP BY subject
4 )b ON a.subject=b.subject5 WHERE a.score=b.max_score
Salin selepas log masuk

6. 列出各门课程成绩最好的两位学生(要求显示字段: 学号,姓名,科目,成绩)

答案

1 SELECT a.stuid, a.name, a.subject, a.score FROM stuscore a
2 WHERE (
3     SELECT COUNT(1) FROM stuscore b
4     WHERE a.subject=b.subject AND b.score>=a.score
5 ) <= 2
6 ORDER BY a.subject ASC, a.score DESC
Salin selepas log masuk

7. 统计如下:

学号

姓名

语文

数学

英语

总分

平均分

答案

1 SELECT stuid 学号, NAME 姓名,
2     SUM(CASE WHEN SUBJECT=&#39;语文&#39; THEN score ELSE 0 END) 语文,
3     SUM(CASE WHEN SUBJECT=&#39;数学&#39; THEN score ELSE 0 END) 数学,
4     SUM(CASE WHEN SUBJECT=&#39;英语&#39; THEN score ELSE 0 END) 英语,
5     SUM(score) 总分, (SUM(score)/COUNT(1)) 平均分
6 FROM stuscore GROUP BY 学号
Salin selepas log masuk

8.列出各门课程的平均成绩(要求显示字段:课程,平均成绩)

答案

1 SELECT SUBJECT, AVG(score) avg_score FROM stuscore GROUP BY SUBJECT
Salin selepas log masuk

9.列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名)

答案

1 SELECT a.*, @var:=@var+1 rank 
2 FROM(
3     SELECT stuid, NAME, score FROM stuscore 
4     WHERE SUBJECT=&#39;数学&#39; ORDER BY score DESC
5 )a, (SELECT @var:=0)b
Salin selepas log masuk

10.列出数学成绩在2-3名的学生(要求显示字段:学号,姓名,科目,成绩)

答案

1 SELECT b.* FROM(
2     SELECT a.* FROM(
3         SELECT stuid, NAME, score FROM stuscore 
4         WHERE SUBJECT=&#39;数学&#39; ORDER BY score DESC 
5         LIMIT 3
6     )a ORDER BY score ASC LIMIT 2
7 )b ORDER BY score DESC
8 
9 #注:当数学成绩只有2条以下数据时,此方法失效!
Salin selepas log masuk

11.求出李四的数学成绩的排名

答案

1 SELECT a.*, @var:=@var+1 rank 
2 FROM(
3     SELECT stuid, NAME, score FROM stuscore 
4     WHERE SUBJECT=&#39;数学&#39; ORDER BY score DESC5 )a, (SELECT @var:=0)b
6 WHERE a.name=&#39;李四&#39;
Salin selepas log masuk

12.统计如下:

课程

不及格(0-59)个

良(60-80)个

优(81-100)个

答案

1 SELECT a.subject 课程,
2     (SELECT COUNT(1) FROM stuscore WHERE score<60 AND SUBJECT=a.subject)不及格,
3     (SELECT COUNT(1) FROM stuscore WHERE score BETWEEN 60 AND 80 AND SUBJECT=a.subject)良,
4     (SELECT COUNT(1) FROM stuscore WHERE score>80 AND SUBJECT=a.subject)优
5 FROM stuscore a GROUP BY SUBJECT
Salin selepas log masuk

Atas ialah kandungan terperinci mysql实现SQL统计的实例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan