![657338707427249d355465edb22884a3.png](https://img-blog.csdnimg.cn/img_convert/657338707427249d355465edb22884a3.png)
1.汇总分析学习:
1.1 如何计算每门课程的平均成绩
select 课程号,avg(成绩)
from score
group by 课程号;
1.2如何计算每门课程的平均成绩,并且平均成绩大于等于80分
select 课程号,avg(成绩)
from score
group by 课程号
having avg(成绩) >= 80;
1.3对查询结果排序:
order by
(desc:降序,从大到小;asc:升序,从小到大)
注意SQL语句运行顺序。
select *
from score
order by 成绩 asc,课程号 desc;
1.4从查询结果中取出指定行:limit
select *
from score
limit 2;
1.5给出相应SQL语句和相应的运行顺序图示:
查询不及格的课程并按课程号从大到小排列;
select *
from course
where 成绩 < 60
order by 课程号;
查询每门课程的平均成绩,结果按平均成绩升序排序。平均成绩相同时,按课程号降序排列;
select avg(成绩) as avg_score,课程号
from course
group by 课程号
order by avg_score asc, 课程号 desc;
1.6如何看懂报错信息:
常见错误:在 group by 中使用了 select 里的别名
常见错误:在 where 中使用聚合函数
常见问题:字符串类型的数字
情景如下:
一列字段最大值是219,但是使用max()函数之后,结果显示是99
这里要注意字符串的比较规则。 同时,这里我们一般会是对数值型的数据进行处理,所以,操作的第一步就是将字符串类型转换为数值型,然后再进行操作。
2.MySQL中cast()函数的用法!
cast()函数:
语法为:Cast(字段名 as 转换的类型 ),
其中类型可以为:
CHAR[(N)] 字符型
DATE 日期型
DATETIME 日期和时间型
DECIMAL float型
SIGNED int
TIME 时间型
select cast(date as char) as date from table1;
select cast(date as date) as date from table1;
select cast(date as datetime) as date from table1;
select cast(12 as decimal(10, 2));
select cast('12' as signed) + 6;
select cast(date as time) as date from table1;
3.sqlzoo练习题
第一部分:
![c077f60d00a425ff0e5d47a3bffa7bc3.png](https://img-blog.csdnimg.cn/img_convert/c077f60d00a425ff0e5d47a3bffa7bc3.png)
![c9e2145a082cba5f40549bcc7036ea41.png](https://img-blog.csdnimg.cn/img_convert/c9e2145a082cba5f40549bcc7036ea41.png)
![3b16fc5ec2fe73c2f2587c2f46037098.png](https://img-blog.csdnimg.cn/img_convert/3b16fc5ec2fe73c2f2587c2f46037098.png)
![774aca4b970f631e9bfecda886d0eabf.png](https://img-blog.csdnimg.cn/img_convert/774aca4b970f631e9bfecda886d0eabf.png)
![51b1f0804f0f94bb14b5d9b2f31c36e8.png](https://img-blog.csdnimg.cn/img_convert/51b1f0804f0f94bb14b5d9b2f31c36e8.png)
![6dcfb4d5cefc1b3f79f5697c6b1ea1af.png](https://img-blog.csdnimg.cn/img_convert/6dcfb4d5cefc1b3f79f5697c6b1ea1af.png)
![609d26cb667af009e80fa194b8c7db57.png](https://img-blog.csdnimg.cn/img_convert/609d26cb667af009e80fa194b8c7db57.png)
![537e64a745273c409efc2294ae3e4460.png](https://img-blog.csdnimg.cn/img_convert/537e64a745273c409efc2294ae3e4460.png)
![c9f61fcb966d41026fb981d98a01b98f.png](https://img-blog.csdnimg.cn/img_convert/c9f61fcb966d41026fb981d98a01b98f.png)
![38fe92a626ca4af0f0d62db83c6ef3cc.png](https://img-blog.csdnimg.cn/img_convert/38fe92a626ca4af0f0d62db83c6ef3cc.png)
![75be0d393bf75e88788a3fc6ab8bcb34.png](https://img-blog.csdnimg.cn/img_convert/75be0d393bf75e88788a3fc6ab8bcb34.png)
![e01ac8ab101d08481dc0d0f9540ab0de.png](https://img-blog.csdnimg.cn/img_convert/e01ac8ab101d08481dc0d0f9540ab0de.png)
![ee26dcfd25e2a8c70d07f525f6d20a57.png](https://img-blog.csdnimg.cn/img_convert/ee26dcfd25e2a8c70d07f525f6d20a57.png)
![1453dd4befc7886b7692de40bd240f17.png](https://img-blog.csdnimg.cn/img_convert/1453dd4befc7886b7692de40bd240f17.png)
第二部分:
![c53203bbd23b2f55ce04b68ac3831f6c.png](https://img-blog.csdnimg.cn/img_convert/c53203bbd23b2f55ce04b68ac3831f6c.png)
![3fe2436978573dc32922406af12e9559.png](https://img-blog.csdnimg.cn/img_convert/3fe2436978573dc32922406af12e9559.png)
![8fb0013be236e4c7efc6162ec06f5e2b.png](https://img-blog.csdnimg.cn/img_convert/8fb0013be236e4c7efc6162ec06f5e2b.png)
![0d57ba738e4226db85bba491e01adc15.png](https://img-blog.csdnimg.cn/img_convert/0d57ba738e4226db85bba491e01adc15.png)
![93870f8197d1647a4e1e42de0d3c567f.png](https://img-blog.csdnimg.cn/img_convert/93870f8197d1647a4e1e42de0d3c567f.png)
![1656293a117883aecc9de555322ba708.png](https://img-blog.csdnimg.cn/img_convert/1656293a117883aecc9de555322ba708.png)
![1140077be9bdf475043b4e02477f452c.png](https://img-blog.csdnimg.cn/img_convert/1140077be9bdf475043b4e02477f452c.png)
最后三道题,还没解决,希望自己把知识需扎实之后,再来补充。如果有同学解决的,可以评论,供我们大家一起学习,感谢。