![b8075e4dd23d304020bdd00ee43a651a.png](https://img-blog.csdnimg.cn/img_convert/b8075e4dd23d304020bdd00ee43a651a.png)
一、入门
1、练习题1
![793b1d5fd87caf74c24d1765f0b8bfb4.png](https://img-blog.csdnimg.cn/img_convert/793b1d5fd87caf74c24d1765f0b8bfb4.png)
问题1:打开安装好的MySQL数据库,输入root用户的密码。
![7f7dcd63631ac930e5a38127644b6802.png](https://img-blog.csdnimg.cn/img_convert/7f7dcd63631ac930e5a38127644b6802.png)
![5540cd167ed9ce89c633ac4e0d7ef90f.png](https://img-blog.csdnimg.cn/img_convert/5540cd167ed9ce89c633ac4e0d7ef90f.png)
问题2:打开客户端navicat,新建连接,连接名自己取。输入root用户密码,主机名一般为默认的localhost,也可以更改为自己想要的名字。点击测试连接,如果出现下图则为连接成功。
![b22d7b7eca89576deb9bdfd1c231c37a.png](https://img-blog.csdnimg.cn/img_convert/b22d7b7eca89576deb9bdfd1c231c37a.png)
2、练习题2
![f1a1241373f5bae7cb23b43f75b4b03b.png](https://img-blog.csdnimg.cn/img_convert/f1a1241373f5bae7cb23b43f75b4b03b.png)
首先创建表,然后添加相应的字段,带钥匙图标的为主键。
![25e2a5a7796c7f26b88d3bbdc3d0f46e.png](https://img-blog.csdnimg.cn/img_convert/25e2a5a7796c7f26b88d3bbdc3d0f46e.png)
成绩表内学号和课程号为联合主键,所以有两把钥匙。
![ceb1eacfaf26a91c9972e2344d222480.png](https://img-blog.csdnimg.cn/img_convert/ceb1eacfaf26a91c9972e2344d222480.png)
![2b0a8481331d21f5675e376b362864f4.png](https://img-blog.csdnimg.cn/img_convert/2b0a8481331d21f5675e376b362864f4.png)
![a64ce45cde28ddc270106c1770ab512e.png](https://img-blog.csdnimg.cn/img_convert/a64ce45cde28ddc270106c1770ab512e.png)
3、练习题3
![0c4d93ac46302d3a79c707933ff68aa7.png](https://img-blog.csdnimg.cn/img_convert/0c4d93ac46302d3a79c707933ff68aa7.png)
插入数据用到语句insert,基本语法为insert into <表名> values(,)
![53e641928321acefcc26dc254a8c2495.png](https://img-blog.csdnimg.cn/img_convert/53e641928321acefcc26dc254a8c2495.png)
![d5d37d52af79179d1ad2e840a948399c.png](https://img-blog.csdnimg.cn/img_convert/d5d37d52af79179d1ad2e840a948399c.png)
4、总结
![fe2837d7dd5814048a431523d5424d07.png](https://img-blog.csdnimg.cn/img_convert/fe2837d7dd5814048a431523d5424d07.png)
![5907f3a22a8ecf8ff4020f00eeecee1f.png](https://img-blog.csdnimg.cn/img_convert/5907f3a22a8ecf8ff4020f00eeecee1f.png)
二、简单查询
1、基本查询语句
![d031e58cd369b1570f3bdf7d612df9c5.png](https://img-blog.csdnimg.cn/img_convert/d031e58cd369b1570f3bdf7d612df9c5.png)
练习:查询学生表
![448b151af2f3d5058b1a1639d687d018.png](https://img-blog.csdnimg.cn/img_convert/448b151af2f3d5058b1a1639d687d018.png)
2、指定查询条件
练习:理解sql运行规则
![57590fc24370261c2ed381f11221bd7a.png](https://img-blog.csdnimg.cn/img_convert/57590fc24370261c2ed381f11221bd7a.png)
查询结果最后执行,先执行from和where后的语句。
3、注释和sql语句注意事项
![a69e60d89d89eb3e7f253239fe426769.png](https://img-blog.csdnimg.cn/img_convert/a69e60d89d89eb3e7f253239fe426769.png)
![1aff4dc2e688d32dd489fa78935f4a51.png](https://img-blog.csdnimg.cn/img_convert/1aff4dc2e688d32dd489fa78935f4a51.png)
4、运算符
![757e76e15d371ec4ce1f40c26afd8a87.png](https://img-blog.csdnimg.cn/img_convert/757e76e15d371ec4ce1f40c26afd8a87.png)
5、字符串模糊查询
![7d795aa381b11107cd722a5f4fccf98e.png](https://img-blog.csdnimg.cn/img_convert/7d795aa381b11107cd722a5f4fccf98e.png)
%代表若干个字符,_代表一个字符
练习:
![53acca3fcb2f4e2e312135c686cb8867.png](https://img-blog.csdnimg.cn/img_convert/53acca3fcb2f4e2e312135c686cb8867.png)
![85069025a21c7b751545d8643e88c092.png](https://img-blog.csdnimg.cn/img_convert/85069025a21c7b751545d8643e88c092.png)
三、汇总分析
1、汇总函数
![6f14dfdbf140268497b78b9d5cb3d33a.png](https://img-blog.csdnimg.cn/img_convert/6f14dfdbf140268497b78b9d5cb3d33a.png)
练习题
![e3c6a7e1cf5170ed6432639405a3518f.png](https://img-blog.csdnimg.cn/img_convert/e3c6a7e1cf5170ed6432639405a3518f.png)
![f4855d100cff5686fdf69567e2a5ab21.png](https://img-blog.csdnimg.cn/img_convert/f4855d100cff5686fdf69567e2a5ab21.png)
![70d1149415f00556cb0c8f5d14e0913e.png](https://img-blog.csdnimg.cn/img_convert/70d1149415f00556cb0c8f5d14e0913e.png)
2、分组
group by
![427fb6d97eba2e0ee2a02bc9b5e2a702.png](https://img-blog.csdnimg.cn/img_convert/427fb6d97eba2e0ee2a02bc9b5e2a702.png)
![55588cb5276390c72b46960ace982e5a.png](https://img-blog.csdnimg.cn/img_convert/55588cb5276390c72b46960ace982e5a.png)
3、对分组结果指定条件
having
![e00dd16abf865d966d413bce8e56dfca.png](https://img-blog.csdnimg.cn/img_convert/e00dd16abf865d966d413bce8e56dfca.png)
![351cb28917dda120b04ad70e3ed24f4c.png](https://img-blog.csdnimg.cn/img_convert/351cb28917dda120b04ad70e3ed24f4c.png)
![2cf4597a049ce454e572322585052ce1.png](https://img-blog.csdnimg.cn/img_convert/2cf4597a049ce454e572322585052ce1.png)
4、用sql解决业务问题
![66756aeeba60025dc549ddfbee28cae3.png](https://img-blog.csdnimg.cn/img_convert/66756aeeba60025dc549ddfbee28cae3.png)
![12b559bfbb0ca1cde52458c84dcd34f1.png](https://img-blog.csdnimg.cn/img_convert/12b559bfbb0ca1cde52458c84dcd34f1.png)
![4d11870947f46d21f57f5d49b420b321.png](https://img-blog.csdnimg.cn/img_convert/4d11870947f46d21f57f5d49b420b321.png)
5、对查询结果排序
order by limit
![829f6f7a25662e7570d84e3c113474b9.png](https://img-blog.csdnimg.cn/img_convert/829f6f7a25662e7570d84e3c113474b9.png)
![6f953d3876052302fdb071851b09e4b3.png](https://img-blog.csdnimg.cn/img_convert/6f953d3876052302fdb071851b09e4b3.png)
![0acc3dc5e59b0ffd00b8f7bad1f9cbb4.png](https://img-blog.csdnimg.cn/img_convert/0acc3dc5e59b0ffd00b8f7bad1f9cbb4.png)
6、报错信息
常见错误:在group by中使用了select里的别名
![b026bb6c30b49d3f9f15431cca8e1448.png](https://img-blog.csdnimg.cn/img_convert/b026bb6c30b49d3f9f15431cca8e1448.png)
常见错误:在where中使用聚合函数,having里可以使用聚合函数
![8d5113f8b070a8360153dcbb3035dca6.png](https://img-blog.csdnimg.cn/img_convert/8d5113f8b070a8360153dcbb3035dca6.png)
四、复杂查询
1、视图
![2b7d5ea250a0bb17960c1908cc64e781.png](https://img-blog.csdnimg.cn/img_convert/2b7d5ea250a0bb17960c1908cc64e781.png)
![419574ea602664881596c6da10835fcb.png](https://img-blog.csdnimg.cn/img_convert/419574ea602664881596c6da10835fcb.png)
2、子查询
子查询是一次性的视图
![78ed76b78606fa448e6a8eb948f46556.png](https://img-blog.csdnimg.cn/img_convert/78ed76b78606fa448e6a8eb948f46556.png)
![42f10ac6918c3b1266b9bd3a4e49818f.png](https://img-blog.csdnimg.cn/img_convert/42f10ac6918c3b1266b9bd3a4e49818f.png)
3、标量子查询
返回单一值的查询,多用于比较运算符
![ea34953ac14283f17e6349f4d5dad7e9.png](https://img-blog.csdnimg.cn/img_convert/ea34953ac14283f17e6349f4d5dad7e9.png)
4、关联子查询
关联子查询条件需要写在子查询中
![784d7f31062b003b338560cb86bba967.png](https://img-blog.csdnimg.cn/img_convert/784d7f31062b003b338560cb86bba967.png)
![0b2384324cfdb4816215765e425fa8d4.png](https://img-blog.csdnimg.cn/img_convert/0b2384324cfdb4816215765e425fa8d4.png)
5、使用SQL解决业务问题
![1ae1f4d602a321f2eb9adff756420630.png](https://img-blog.csdnimg.cn/img_convert/1ae1f4d602a321f2eb9adff756420630.png)
五、多表查询
1、表的加法
union
![3b1535936abd193e1ce4e1e7cf8affe8.png](https://img-blog.csdnimg.cn/img_convert/3b1535936abd193e1ce4e1e7cf8affe8.png)
2、表的联结(join)
内联结inner join
![5cd1141ea0c38bc09604845cf6795bff.png](https://img-blog.csdnimg.cn/img_convert/5cd1141ea0c38bc09604845cf6795bff.png)
左联结(left join)
![63ceb4b26aadc03a3ef54dcd921a2452.png](https://img-blog.csdnimg.cn/img_convert/63ceb4b26aadc03a3ef54dcd921a2452.png)
右联结(right join)
![01e656a6c66d164d30cfad4b54bb62fb.png](https://img-blog.csdnimg.cn/img_convert/01e656a6c66d164d30cfad4b54bb62fb.png)
全联结(full join)
![5a81e4d4cc2be24bdf322f2ba3e3b6ed.png](https://img-blog.csdnimg.cn/img_convert/5a81e4d4cc2be24bdf322f2ba3e3b6ed.png)
![bb2389ff462890cfa7534a070f4c234b.png](https://img-blog.csdnimg.cn/img_convert/bb2389ff462890cfa7534a070f4c234b.png)
![25dfcca6aa82c387162e750a6086dc2e.png](https://img-blog.csdnimg.cn/img_convert/25dfcca6aa82c387162e750a6086dc2e.png)
![488f52768af3e6a612c04b0e7358ebfd.png](https://img-blog.csdnimg.cn/img_convert/488f52768af3e6a612c04b0e7358ebfd.png)
![193cae8b00ea3413afe9732ab69b8889.png](https://img-blog.csdnimg.cn/img_convert/193cae8b00ea3413afe9732ab69b8889.png)
4、case表达式
![56c422aa6e6bb005ece5b09e98373c91.png](https://img-blog.csdnimg.cn/img_convert/56c422aa6e6bb005ece5b09e98373c91.png)
![ae0164b0f94fbddb44bad9bc931986b5.png](https://img-blog.csdnimg.cn/img_convert/ae0164b0f94fbddb44bad9bc931986b5.png)
![c2685db75ced570423337a1ab0ffba0f.png](https://img-blog.csdnimg.cn/img_convert/c2685db75ced570423337a1ab0ffba0f.png)
六、求职面试题总结
![49f57ab0acdf9becd0ba5995c27d1f31.png](https://img-blog.csdnimg.cn/img_convert/49f57ab0acdf9becd0ba5995c27d1f31.png)
![0cee80ada98d5f3b728b11771f695e9e.png](https://img-blog.csdnimg.cn/img_convert/0cee80ada98d5f3b728b11771f695e9e.png)
![ddfa16d06f7ed05ab4b9328ed5071560.png](https://img-blog.csdnimg.cn/img_convert/ddfa16d06f7ed05ab4b9328ed5071560.png)