mysql精彩案例
来源:互联网 发布:莫烦 tensorflow 编辑:程序博客网 时间:2024/05/16 03:09
例:查询出2门及2门以上不及格者的平均成绩
mysql> select * from a;
+------+---------+-------+
| name | subject | score |
+------+---------+-------+
| 张三 |数学 | 90 |
| 张三 |语文 | 50 |
| 张三 |地理 | 40 |
| 王五 |政治 | 30 |
| 李四 |政治 | 45 |
| 李四 |语文 | 55 |
+------+---------+-------+
6 rows in set (0.00 sec)
答案分析:
//每个人的评价成绩
mysql> select name, avg(score) from a
-> group by name;
+------+------------+
| name | avg(score) |
+------+------------+
| 张三 | 60 |
| 李四 | 50 |
| 王五 | 30 |
+------+------------+
3 rows in set (0.00 sec)
//每个人有多少科目
mysql> select name, count(subject) froma
-> group by name;
+------+----------------+
| name | count(subject) |
+------+----------------+
| 张三 | 3 |
| 李四 | 2 |
| 王五 | 1 |
+------+----------------+
3 rows in set (0.00 sec)
//不及格成绩有两门以上的
mysql> select name, count(subject) froma
-> where score<60
-> group by name
-> having count(subject)>=2;
+------+----------------+
| name | count(subject) |
+------+----------------+
| 张三 | 2 |
| 李四 | 2 |
+------+----------------+
2 rows in set (0.00 sec)
//不及格成绩有两门以上的,不及格的评价成绩
mysql> select name,count(subject),avg(score) from a
-> where score<60
-> group by name
-> having count(subject)>=2;
+------+----------------+------------+
| name | count(subject) | avg(score) |
+------+----------------+------------+
| 张三 | 2 | 45 |
| 李四 | 2 | 50 |
+------+----------------+------------+
2 rows in set (0.00 sec)
//两门以上不及格的的人
mysql> select name from a
-> where score<60
-> group by name
-> having count(subject)>=2;
+------+
| name |
+------+
| 张三 |
| 李四 |
+------+
2 rows in set (0.00 sec)
//
mysql> select name ,avg(score) from awhere name in (select name from a where sc
ore<60 group by name havingcount(subject)>=2) group by name;
+------+------------+
| name | avg(score) |
+------+------------+
| 张三 | 60 |
| 李四 | 50 |
+------+------------+
2 rows in set (0.00 sec)
mysql>
- mysql精彩案例
- html5精彩案例
- Three.js精彩案例
- 用vb.net精彩案例
- JS特效模板精彩案例!
- PHP案例-精彩商城教学
- 一个精彩的多线程 同步 案例
- Web应用界面设计精彩案例分享
- Web应用界面设计精彩案例分享地址
- Web应用界面设计精彩案例分享
- 精彩案例-悬浮在桌面上的照相机
- 精彩案例-悬浮在桌面上的照相机
- HTML5 CSS3实现的精彩案例
- 翻转课堂十大精彩案例
- 各大网站面试精彩案例
- SQL优化案例精彩连载(一)
- Linux网络备份MYSQL(精彩)
- linux下批量修改文件名精彩解答案例分享
- Kernel Korner - Sleeping in the Kernel
- 聚类算法总结
- ibatis3.0配置
- 黑马程序员_java的IO流(对第十九课字节流及转换流的总结二)
- 海盗分酒(数学问题)
- mysql精彩案例
- hdu_2040_亲和数_解题报告
- Arduino学习 - 2 搭建环境
- weka 聚类算法总结
- phpshell_ddos攻击型webshell
- android paint(画笔)及Canvas(画布)的使用
- MySQL添加用户方法
- mysql外键
- PHP环境的搭建和数据库的建立