玖叶教程网

前端编程开发入门

Python(二十三)MySQL提升

今天介绍四个程序员岗位方向:摸鱼、开发、测试、运维。

摸鱼:本来一天可以完成的事情,花好几天的时间做,各个行业都有,实力有强有弱,个性十足。

开发:能够使用一种(或多种)编程语言,完成某个产品(或项目),通常更关注制造过程,不关心运营生命周期。

测试:使用各种测试技术和工具来测试和发现软件中存在的缺陷,从而让开发者更好的优化产品,让用户更加安全顺畅的使用。

运维:管理(或维护)系统、主机及产品,通常更关心运营生命周期,不关心制造过程,相比之下,心理素质较高。

科普结束,进入正题,今天讲的内容有MySQL单表查询、MySQL多表查询、MySQL函数、MySQL优化。

1. 单表查询:

1.1. 查询所有记录:

select * from mr;:查询mr表中的所有字段。


select sex from mr;:查询mr表中的sex字段。

1.2. where条件查询:

select * from s_c where s_name='mr';:查询s_c表中,s_name='mr'的所有记录。

1.3. where搭配逻辑运算符查询:

select * from s_c where c_name='python' and s_name='mr';查询s_c表中,c_name='python'和s_name='mr'的所有记录。


select * from s_c where c_name='python' and s_name='mr';:查询s_c表中,c_name='python'和s_name='mr'的所有记录。
select * from s_c where c_name='python' or s_name='mr';:查询s_c表中,c_name='python'或s_name='mr'的所有记录。

1.4. 利用别名:

select sd.s_id from stu_detail as sd;:查询stu_detail(别名为sd)
表中的s_id字段,其中as可省。

1.5. where的模糊查询:

select * from s_c where c_name like 'p%':%表示通配符,在s_c表中匹配c_name以p开头的数据。

2. 多表查询:

2.1. 联表查询:

2.1.1. 内连接:

select * from department inner join student;:查询department和student表中的所有数据并一一组合,也可以写成select * from department,student


但因为上述代码现实的结果过于冗余,不是我们想要的,所以需写成下面这种方式:
select * from department inner join student on department.d_id=student.dept_id;或写成select * from department,student where department.d_id=student.dept_id;:查询department表中d_id等于student表中dept_id的所有数据。on和where需要对应前面的inner join写法,都表示查询条件。

select * from student inner join stu_course on student.s_id=stu_course.s_id inner join course on stu_course.c_id=course.c_id;:查询student表中s_id等于stu_course表中s_id,与stu_course表中c_id等于course表中c_id的所有数据。

2.1.2. 外连接:

2.1.2.1. 左连接:

select * from department dt left join student s on dt.d_id=s.dept_id;以左边的学院表为主,当department表中有Null记录时,不影响查询。

2.1.2.2. 右连接:

select * from department dt right join student s on dt.d_id=s.dept_id;以右边的学生表为主,当student表中有Null记录时,不影响查询。

2.2. 子表查询:

select * from course inner join (select s_name,c_id from student inner join stu_couse on student.s_id=stu_course.s_id) as e on course.c_id=e.c_id:e表示临时表的别名,其中临时表查询字段一定不能用*,否则会报无法找到查询的条件错误。代码思路:将查询结果作为一张临时表与另一张表建立内连接,获取想要的查询记录。

2.3. 查询限制:

排序:order by
select * from department order by d_id asc:查询department的表数据,并按d_id字段进行升序排序操作,不加asc也是默认升序。如select * from department order by d_id
select * from department order by d_id desc:查询department的表数据,并按d_id字段进行降序排序操作。


限制函数:limit
select * from department order by d_id limit 3:查询department的表数据,并按d_id字段进行降序排序操作,取前三。

select * from department order by d_id limit 1,2:查询department的表数据,并按d_id字段进行降序排序操作,取前第二、第三,默认索引是从0开始的

2.4. 分组查询:

select s_name,count(*) from stu_course inner join student on stu_course.s_id=student.s_id inner join course on stu_course.c_id=course.c_id group by s_name;:查询stu_course表中s_id与student表中s_id相等以及course表中c_id与stu_course表中c_id相等的s_name,以及根据s_name统计的数值数据。


select s_name,count(*) from stu_course inner join student on stu_course.s_id=student.s_id inner join course on stu_course.c_id=course.c_id group by s_name having s_name='mr';:查询stu_course表中s_id与student表中s_id相等以及course表中c_id与stu_course表中c_id相等的s_name,以及根据s_name统计的数值数据。

3. MySQL函数:

ifnull
student表中数据如图所示:

select s_name,ifnull(dept_id,1) from student;:如果dept_id为null,则将其数据以1输出。
distinct:字段去重。
select distinct dept_id from student;:去除重复的学院号(dept_id)

select max(sd_age) from stu_detail;:查询表stu_detail中sd_age的最大值。

类似的还有min、abs、avg、round、sum。

4. MySQL优化:


文章到这里就结束了!希望大家能多多支持Python(系列)!六个月带大家学会Python,私聊我,可以问关于本文章的问题.以后每天都会发布新的文章,一个陪伴你学习Python的新青年!不管多忙都会更新下去,一起加油!

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言