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服务器,缓存) ...
随机推荐
- asp.net将数据库中的数据赋给DropDownList
当你选定一项进行其他操作时会重新绑定dropdownlist,这样会重新回到第一项,在page_load里加上判断if(!IsPostBack){'这里是你需要绑定dropdownlist的代码'}. ...
- java中的CAS
转自:http://www.blogjava.net/mstar/archive/2013/04/24/398351.html Atomic 从JDK5开始, java.util.concurrent ...
- Java集合类之LinkedList链表
package com.test; import java.util.*; public class Demo7_3 { public static void main(String[] args) ...
- IIS UrlWriter配置(asp.net)
前提在建虚拟目录或网站时注意以下设置第一步:下载URLRewriter 添加URLRewriter和ActionlessForm(不添加只能在VS实现,IIS下会找不到页面). 第二步:配置web.c ...
- hdu4722Good Numbers(dp)
链接 这题规律其实挺明显的 打表找规律估计都可以 正规点就是DP 算出第N位所包含的good number的数量 如果给出的数是N+1位 就枚举各位上比原来小的数 加上下一位的dp值 一个i写成g了 ...
- bzoj3514
好题+数据结构神题+感人肺腑pascal被卡系列,我下面的代码几乎写到最优可怎耐bzoj上pascal开的是O1,c++开的是O2,这怎么可能跑得过!!!还是说说方法吧,这是一道算贡献的好题,因为我们 ...
- Wordpress Jigoshop插件路径泄露漏洞
漏洞名称: Wordpress Jigoshop插件路径泄露漏洞 CNNVD编号: CNNVD-201311-109 发布时间: 2013-11-12 更新时间: 2013-11-12 危害等级: ...
- 用Remastersys定制自己的Ubuntu安装光盘
这两天因为要做Ubuntu的平台移植,一直在给自己电脑上的Ubuntu系统装各种软件,其间几次将内核破坏,导致不得不重装系统,经过几次痛苦的等待,痛定思痛,决定试一试能不能将自己的Ubuntu系统定制 ...
- 软件介绍(apache lighttpd nginx)
一.软件介绍(apache lighttpd nginx) 1. lighttpd Lighttpd是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点.lighttpd是众多 ...
- jquery easyui datebox 的使用
看了jquery easyui databox的官方api,还可以加入倒是很简单,但是想要获得他的值和修改值就很费劲,不知道怎么弄,试了n次终于搞定.这里总结一下,供有相同问题的人查询. 1. 官方a ...