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. MongoDB实战指南(一):大数据与云计算

    1.1 什么大数据 具体来说,大数据技术涉及到数据的创造,存储,获取和分析,大数据的主要特点有下面几个: 数据量大.一个典型的PC机载2000年前后其存储空间可能有10GB,今天facebook一天增 ...

  2. easyui源码翻译1.32--LinkButton(按钮)

    前言 使用$.fn.linkbutton.defaults重写默认值对象.下载该插件翻译源码 按钮组件使用超链接按钮创建.它使用一个普通的<a>标签进行展示.它可以同时显示一个图标和文本, ...

  3. SSO(转)

    一.介绍       主站下有多个子系统,每次登录主系统,跳转到子系统时,又需要重新登录: 子系统与主系统都有各自的用户信息表:各个系统的用户角色.权限也各不相同: 二.目的       每次登录主系 ...

  4. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  5. 学习笔记-[Maven实战]-第二章:Maven安装和配置

    在windows上安装Maven 1.检查JDK安装:在CMD下运行以下命令来检查JAVA安装情况: 命令: C:\Documents and Settings\Administrator>ec ...

  6. linux移植简介[MS2]

    一:修改Makefile 1添加编译选项,2添加时钟 二:图形化配置(make menuconfig) 此时生成zlmage,  可以跑起来 三:对NandFlash配置,yaffs2 文件系统配置, ...

  7. $destroy

    ng-view 路由切换会触发 $destroy

  8. 使用haproxy做负载均衡时保持客户端真实的IP

    haproxy里添加设置项 option forwardfor option httpclose apache的日志格式修改 LogFormat "MY IP=%{X-Forwarded-F ...

  9. POJ 2240 Arbitrage spfa 判正环

    d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...

  10. Code First to an Existing Database

    Code First to an Existing Database https://msdn.microsoft.com/en-us/data/jj200620 https://weblogs.as ...