1. 基础数据表

学生成绩表(stuscore):

姓名:name

课程:subject

分数:score

学号:stuid

张三

数学

89

1

张三

语文

80

1

张三

英语

70

1

李四

数学

90

2

李四

语文

70

2

李四

英语

80

2

 

2. 问题:

  1. 计算每个人的总成绩并排名,并按总成绩降序排列(要求显示字段:学号,姓名,总成绩)
  2. 计算每个人单科的最高成绩(要求显示字段: 学号,姓名,课程,最高成绩)
  3. 列出各门课程成绩最好的学生(要求显示字段: 学号,姓名, 科目,成绩)
  4. 列出各门课程成绩最好的两位学生(要求显示字段: 学号,姓名,科目,成绩)
  5. 列出各门课程的平均成绩,并按平均成绩降序排列(要求显示字段:课程,平均成绩)
  6. 列出总分成绩的排名(要求显示字段:学号,姓名,成绩,排名)
  7. 列出数学成绩在2-3名的学生(要求显示字段:学号,姓名,科目,成绩)
  8. 求出李四的数学成绩的排名
  9. 统计如下:

学号

姓名

语文

数学

英语

总分

平均分

10. 统计如下:

课程

不及格(0-59)个

良(60-80)个

优(81-100)个

3. 参考答案

  1. select stuid, name, sum(score) as sum_score from stuscore group by stuid order by sum_score desc;
  2. select stuid, name, sub, score from stuscore where score in (select max(score) from stuscore group by stuid);
  3. select stuid, name, sub, score from stuscore where score in (select max(score) from stuscore group by sub);
  4. select a.* from stuscore a where exists (select count(*) from stuscore where sub = a.sub and score > a.score having count(*) < 2) order by a.sub, a.score desc;
  5. select sub, avg(score) as avg_score from stuscore group by sub order by avg_score desc;
  6. select (select (count(stuid)+1 from (select stuid, sum(score) as sum_score from stuscore group by stuid) as A where A.sum_score > B.sum_score) as seq, B.stuid, B.name, B.sum_score from (select stuid, name, sum(score) as sum_score from stuscore group by stuid) as B order by sum_score desc;
  7. select stuid, name, score, sub from stuscore where sub = 'math' order by score desc limit 1, 3;
  8. select (select (count(stuid)+1 from (select stuid, score from stuscore where sub = 'math') as A where A.score > B.score) as seq, B.stuid, B.name, B.sum_score from (select stuid, name, sub, score from stuscore where sub = 'math' and name = '李四') as B;
  9. select stuid, name, sum(case when sub = 'chinese' then score else 0 end) as chinese, sum(case when sub = 'math' then score else 0 end) as math, sum(case when sub = 'english' then score else 0 end) as english, sum(score) as sum_score, avg(score) as avg_score from stuscore group by stuid;
  10. select sub, sum(case when score < 60 then 1 else 0 end) as lower_60, sum(case when score < 81 and score > 59 then 1 else 0 end) as between_60_80, sum(case when score > 80 then 1 else 0 end) as higher_80 from stuscore group by sub;

mysql查询语句举例的更多相关文章

  1. MySQL查询语句执行过程及性能优化(JOIN/ORDER BY)-图

    http://blog.csdn.net/iefreer/article/details/12622097 MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY) 标签 ...

  2. mysql查询语句,通过limit来限制查询的行数。

    mysql查询语句,通过limit来限制查询的行数. 例如: select name from usertb where age > 20 limit 0, 1; //限制从第一条开始,显示1条 ...

  3. MYSQL查询语句大全集锦

    MYSQL查询语句大全集锦 1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; 2:2.创建一个数据库MYSQLDATA mysql> C ...

  4. MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介

    网站或服务的性能关键点很大程度在于数据库的设计(假设你选择了合适的语言开发框架)以及如何查询数据上. 我们知道MySQL的性能优化方法,一般有建立索引.规避复杂联合查询.设置冗余字段.建立中间表.查询 ...

  5. MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY)

    在上一篇文章MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介中介绍了EXPLAIN语句,并举了一个慢查询例子:

  6. mysql查询语句集

    1. mysql 查询出某字段的值不为空的语句 1.不为空 select * from table where id <> ""; select * from tabl ...

  7. [转]MySQL查询语句执行过程详解

    Mysql查询语句执行原理 数据库查询语句如何执行?语法分析:首先进行语法分析,对使用sql表示的查询进行语法分析,生成查询语法分析树.语义检查:检查sql中所涉及的对象以及是否在数据库中存在,用户是 ...

  8. Mysql查询语句中字符型字段不区分大小写解决方法

    项目中和前端联调的时候,发现Mysql查询语句中字符型字段值过滤是不区分大小写的,之前没有关注过这个设置,特意去网上看了下,原因是Mysql中“COLLATE”属性区分大小写,而该属性默认值为“utf ...

  9. php面试专题---MYSQL查询语句优化

    php面试专题---MYSQL查询语句优化 一.总结 一句话总结: mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存) ...

随机推荐

  1. 同步两台linux服务器时间同步方案

    Linux自带了ntp服务 -- /etc/init.d/ntpd,这个服务不仅可以设置让本机和某台/某些机器做时间同步,他本身还可以扮演一个time server的角色,让其他机器和他同步时间. 配 ...

  2. C++的表驱动法

    目的:使用表驱动法,替换复杂的if/else和switch/case语句. 说明:JS 等其他语言也都支持的. 表驱动发示例:http://blog.csdn.net/zhouyulu/article ...

  3. 在Android手机上安装linux系统

    在anroid手机中安装fedora系统.记住不只是教你安装fedora系统. 需要的备注与软件 1.一个已经root的Android手机,记住是root后的,root后的,root后的.(重要的事情 ...

  4. RxJava开发精要2-为什么是Observables?

    原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...

  5. tomcat docBase 和 path

    <Context docBase="zjzc-web-api" path="/api" reloadable="false"/> ...

  6. 【HDOJ】2037 今年暑假不AC

    qsort排序后DP,水题.注意,数组开大点儿,把时间理解为0~23,开太小会wa. #include <stdio.h> #include <stdlib.h> #defin ...

  7. 用jQuery 处理XML-- jQuery与XML

    jQuery与XML 快而强的遍历系统,华丽丽的选择器语法,这或许是jQuery 那么流行的原因.当然它还有详尽的文档.它主要是用来处理HTML的,但在这里妳会看到如何应用到XML. 使用jQuery ...

  8. mysql 查看死锁和去除死锁

    1.查询是否锁表show OPEN TABLES where In_use > 0; 2.查询进程 show processlist 3.  查询到相对应的进程,然后 kill id 验证(ki ...

  9. Nginx实现七层负载均衡配置指导

    本文描述了如何使用Nginx实现在应用层实现7层负载均衡功能,Nginx支持虚拟主机,可以按照轮询,IP哈希,URL哈希,权重方式对后端服务器做负载均衡,还支持后端服务器健康检查功能.废话不多说,详细 ...

  10. CLR via C# 读书笔记 6-2 不同AppDomain之间的通信 z

    跨AppDomain通信有两种方式 1.Marshal By reference : 传递引用 2.Marshal By Value : 把需要传递的对象 通过序列化反序列化的方式传递过去(值拷贝) ...