mysql查询语句举例
1. 基础数据表
学生成绩表(stuscore):
|
姓名:name |
课程:subject |
分数:score |
学号:stuid |
|
张三 |
数学 |
89 |
1 |
|
张三 |
语文 |
80 |
1 |
|
张三 |
英语 |
70 |
1 |
|
李四 |
数学 |
90 |
2 |
|
李四 |
语文 |
70 |
2 |
|
李四 |
英语 |
80 |
2 |

2. 问题:
- 计算每个人的总成绩并排名,并按总成绩降序排列(要求显示字段:学号,姓名,总成绩)
- 计算每个人单科的最高成绩(要求显示字段: 学号,姓名,课程,最高成绩)
- 列出各门课程成绩最好的学生(要求显示字段: 学号,姓名, 科目,成绩)
- 列出各门课程成绩最好的两位学生(要求显示字段: 学号,姓名,科目,成绩)
- 列出各门课程的平均成绩,并按平均成绩降序排列(要求显示字段:课程,平均成绩)
- 列出总分成绩的排名(要求显示字段:学号,姓名,成绩,排名)
- 列出数学成绩在2-3名的学生(要求显示字段:学号,姓名,科目,成绩)
- 求出李四的数学成绩的排名
- 统计如下:
|
学号 |
姓名 |
语文 |
数学 |
英语 |
总分 |
平均分 |
10. 统计如下:
|
课程 |
不及格(0-59)个 |
良(60-80)个 |
优(81-100)个 |
3. 参考答案
- select stuid, name, sum(score) as sum_score from stuscore group by stuid order by sum_score desc;
- select stuid, name, sub, score from stuscore where score in (select max(score) from stuscore group by stuid);
- select stuid, name, sub, score from stuscore where score in (select max(score) from stuscore group by sub);
- 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;
- select sub, avg(score) as avg_score from stuscore group by sub order by avg_score desc;
- 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;
- select stuid, name, score, sub from stuscore where sub = 'math' order by score desc limit 1, 3;
- 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;
- 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;
- 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查询语句举例的更多相关文章
- MySQL查询语句执行过程及性能优化(JOIN/ORDER BY)-图
http://blog.csdn.net/iefreer/article/details/12622097 MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY) 标签 ...
- mysql查询语句,通过limit来限制查询的行数。
mysql查询语句,通过limit来限制查询的行数. 例如: select name from usertb where age > 20 limit 0, 1; //限制从第一条开始,显示1条 ...
- MYSQL查询语句大全集锦
MYSQL查询语句大全集锦 1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; 2:2.创建一个数据库MYSQLDATA mysql> C ...
- MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介
网站或服务的性能关键点很大程度在于数据库的设计(假设你选择了合适的语言开发框架)以及如何查询数据上. 我们知道MySQL的性能优化方法,一般有建立索引.规避复杂联合查询.设置冗余字段.建立中间表.查询 ...
- MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY)
在上一篇文章MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介中介绍了EXPLAIN语句,并举了一个慢查询例子:
- mysql查询语句集
1. mysql 查询出某字段的值不为空的语句 1.不为空 select * from table where id <> ""; select * from tabl ...
- [转]MySQL查询语句执行过程详解
Mysql查询语句执行原理 数据库查询语句如何执行?语法分析:首先进行语法分析,对使用sql表示的查询进行语法分析,生成查询语法分析树.语义检查:检查sql中所涉及的对象以及是否在数据库中存在,用户是 ...
- Mysql查询语句中字符型字段不区分大小写解决方法
项目中和前端联调的时候,发现Mysql查询语句中字符型字段值过滤是不区分大小写的,之前没有关注过这个设置,特意去网上看了下,原因是Mysql中“COLLATE”属性区分大小写,而该属性默认值为“utf ...
- php面试专题---MYSQL查询语句优化
php面试专题---MYSQL查询语句优化 一.总结 一句话总结: mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存) ...
随机推荐
- 1842-A. Broj
#include <iostream> using namespace std; int main() { int n; cin>>n; if(n>0&& ...
- Mybatis bug修正
http://1358440610-qq-com.iteye.com/blog/1827391
- yii
2008年出现的一个以php为基础的框架,特点是:高性能框架.代码重用性.速度非常快(改完代码后直接刷新就可以展示修改后的页面).有小物件.登录组件.日志组件等等. main.php配置与数据库相连的 ...
- linux如何安装jdk
一.安装 创建安装目录,在/usr/java下建立安装路径,并将文件考到该路径下: # mkdir /usr/java 1.jdk-6u11-linux-i586.bin 这个是自解压的文件,在lin ...
- html5 高级动画精灵
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- [译]GotW #4 Class Mechanics
你对写一个类的细节有多在行?这条款不仅注重公然的错误,更多的是一种专业的风格.了解这些原则将会帮助你设计易于使用和易于管理的类. JG Question 1. 什么使得接口“容易正确使用,错误使用却很 ...
- 【HDOJ】2045 不容易系列之(3)—— LELE的RPG难题
着色问题,递推,当超过3个块时,规律明显,此时可以是n-2的头尾重复+与头尾不同颜色,也可以是n-1+与头尾均不相同眼色情况.经典递推.注意long long. #include <stdio. ...
- MS dos版本
1981年,MS-DOS 1.0发行,作为IBM PC的操作系统进行捆绑发售,支持16k内存及160k的5寸软盘.在硬件昂贵,操作系统基本属于送硬件奉送的年代,谁也没能想到,微软公司竟会从这个不起眼的 ...
- Dell笔记本禁用触摸板的方法
一·找到触摸板驱动所在的文件夹(其他型号 的本本,请自己探索一下,找到驱动在哪就行),一般 在 C:\program files\delltpad 中(若没有请下载安 装 ),如图: 二·双击 Del ...
- HDU 4746 Mophues 莫比乌斯反演
分析: http://blog.csdn.net/acdreamers/article/details/12871643 分析参见这一篇 http://wenku.baidu.com/view/fbe ...