今日继续学习MySql数据库部分,这块用的比较多的是带有各种条件的select。
目录
1、students表准备(查询)
2、字段的别名
3、表的别名
4、distinct 过滤重复记录
5、where子句
6、select 查询的基本规律
7、比较运算法
8、逻辑运算符
9、like模糊查询
10、范围查找
11、空判断
12、where语句在update、delete后
13、order by排序
单字段排序:
多字段排序:
14、where结合order by
15、聚合函数
(1)count函数
(2)max最大值
(3)min查询最小值
(4)求和sum
(5)avg求平均
16、分组 group by
17、where和order by 、order by的顺序
18、分组后筛选
19、where与having 的区别:
20、分页limit
21、分页
22、求总页数
23、数据准备(多表查询)
24、连接查询介绍
24.1 内连接
24.1.1 练习
24.1.2 隐式内连接
24.1.3 内连接指定显示部分字段
24.1.4 显示表的别名
24.1.5 带有where的内连接
编辑
24.1.6 多表内连查询
24.2 左连接
24.3 右连接
1、students表准备(查询)
-- 准备students表
drop table if exists students;/*创建学生表students*/create table students(studentNO varchar(10) primary key,/*学号*/name varchar(10),sex varchar(1),hometown varchar(20),age tinyint,class varchar(10),card varchar(18));insert into students values
('001','王昭君','女','北京',30,'1班','110214567897654367'),('002','诸葛亮','男','上海',29,'2班','310214567897654367'),('003','张飞','男','南京',30,'3班','320214567897654367'),('004','白起','男','安徽',35,'4班','340214567897654367'),('005','大乔','女','天津',28,'3班','120214567897654367'),('006','孙尚香','女','河北',25,'1班','130214567897654367'),('007','百里玄策','男','山西',39,'2班','140214567897654367'),('008','小乔','女','河南',25,'3班',null),('009','百里守约','男','湖南',31,'1班',''),('010','妲己','女','广东',24,'2班','440214567897654367'),('011','李白','男','北京',30,'3班','110214567897654367'),('012','孙膑','男','新疆',36,'4班','650214567897654367');select * from students;
2、字段的别名
通过as 给字段添加别名(这里的as 可写可不写)
-- 通过as 给字段添加别名(这里的as 可写可不写)
select card as 身份证,name as 姓名 from students;select card 身份证, name 姓名 from students;
3、表的别名
-- 给表起别名
select * from sttudents as stu;select * from students stu;
4、distinct 过滤重复记录
distinct 是跟在select后面的,过滤的是 select 查询结果中的重复记录。
select DISTINCT sex from students;select DISTINCT sex,class from students;
5、where子句
where 后面跟条件,表示有选择的查询。
select * from students where studentNo = '001';select name 姓名, class 班级 from students where age = 30;
6、select 查询的基本规律
select * 或 select 字段名 控制了查询返回什么样的字段(列)
where条件 控制了查询返回什么样的记录(行)
7、比较运算法
-- 比较运算符
select * from students where name = '小乔';
select * from students where age <= 30;
select * from students where hometown <> '北京';
select * from students where hometown != '北京';
8、逻辑运算符
and、or、not
select * from students where age < 30 and sex = '女';select * from students where sex = '女' or class ='1班';select * from students where not hometown = '天津';
练习题:
-- 课堂练习
select * from students where hometown = '河南' or hometown = '河北';
select * from students where class ='1班' and hometown ='北京';
select * from students where age <> 30;
select * from students where not age = 30;
9、like模糊查询
% 表示任意多个任意字符
_ 表示一个任意字符
select * from students where name like '孙%';
select * from students where name like '孙_';
练习题:
select * from students where name like '__';
select * from students where name like '白%' and age >30;
select * from students where studentNo like '%1';
10、范围查找
查找连续范围 between ... and 、查找非连续范围 in。
-- 查找非连续范围
select * from students where hometown in ('北京','上海','广东');-- 查找连续范围
select * from students where between 25 and 30;
练习题:
-- 练习题
select * from students where age in (20,25,30) and sex ='女';
select * from students where not age between 25 and 30;
11、空判断
-- 空判断
select * from students where card is null;
select * from students where card is not null;select * from students where card ='';
12、where语句在update、delete后
-- where 可以用在update和delete后面
update students set age = 18 where name ='孙尚香' and class='1班';
select * from students where name = '孙尚香';
delete from students where class='1班' and age > 30;
练习题:
-- 练习
select * from students;
update students set class='1班' where name like '孙%';
delete FROM students where sex='男' and (age between 20 and 30);
13、order by排序
单字段排序:
-- ASC 默认; DESC降序
select * from students ORDER BY age DESC;
多字段排序:
-- 先按照age排序降序,如果age相同的情况,再按照studentNo升序。
select * from students order by age DESC,studentNO;
14、where结合order by
select * from students where sex='男' order by class,studentNO DESC;
15、聚合函数
(1)count函数
count 求select 返回的记录总数。
-- 查询学生总数
select count(*) from students;select count(DISTINCT class) from students;select count(DISTINCT sex) from students;-- 查询女同学人数
select count(*) from students where sex='女';
(2)max最大值
-- 查询全班最大年龄
select max(age) FROM students;-- 查询女生最大年龄
select max(age) FROM students where sex='女';
聚合函数不能用到where后面的条件里面。
(3)min查询最小值
(4)求和sum
-- 查询students中的年龄总和
select sum(age) from students;
(5)avg求平均
avg()计算,是忽略null行的。
-- 求所有学生的平均年龄
select avg(age) from students;
练习题
-- 练习题
select max(age),min(age),avg(age) from students;select count(*) from students WHERE class='1班';select count(*) from students WHERE class='3班' and age < 30;
16、分组 group by
-- 数据分组
select sex, count(*) from students GROUP BY sex;-- 查询1班,不同性别学生的数量
select sex,count(*) from students where class='1班' GROUP BY sex;-- 用数据分组方法,统计各个班级学生总数、平均年龄、最大年龄、最小年龄。
select class,count(*),avg(age),max(age),min(age) from students GROUP BY class;
-- 统计各个班级学生总数、平均年龄、最大年龄、最小年龄。但不统计3班,统计结果按班级名称从大到小排序。
select class, count(*),avg(age),max(age),min(age) from students where class != '3班' GROUP BY class DESC;
17、where和order by 、order by的顺序
18、分组后筛选
having 后面可以使用聚合函数。
where后面不可以使用聚合函数。
-- 选筛选,再count。
select count(*) from students WHERE sex='男';
-- 先分组聚合,再选取符合条件的。
select count(*) from students GROUP BY sex having sex = '男';
-- 求班级人数大于3人的班级
select class,count(*) from students GROUP BY class having count(*)>3;
19、where与having 的区别:
练习:
-- 查询班级总人数大于2人的班级名称及班级对应的总人数。
select class,count(*) from students GROUP BY class having count(*) > 2;-- 查询平均年龄大于30岁的班级名称和班级总人数。
select class,avg(age),count(*) from students GROUP BY class having avg(age)>30;
20、分页limit
-- 查询前3行记录 0表示从第1行开始,3表示显示3行。
select * from students LIMIT 0,3; -- 0可以省略
select * from students LIMIT 3;
-- 查询年龄最大同学的name
select * from students order by age DESC limit 1;-- 查询年龄最小的女同学信息
select * from students where sex='女' order by age limit 1;
21、分页
-- 每页显示4条记录,查询第3页的数据。
select * from students limit 8,4;
22、求总页数
-- 每页显示5条记录,一共需要多少页
select count(*) from students;
练习题:
-- 第一页
select * from students limit 5;
-- 第二页
select * from students limit 5,5;
-- 第三页
select * from students limit 10,5;
23、数据准备(多表查询)
/* 课程表 */create table courses (courseNo int unsigned primary key auto_increment,coursename varchar(10)
);insert into courses (coursename) values ('数据库'), ('html'), ('linux'), ('系统测试'), ('单元测试'), ('测试过程'), ('python');/* 成绩表 */create table scores (id int unsigned primary key auto_increment,courseNo int unsigned,studentNo varchar(10),score tinyint
);insert into scores (courseNo, studentNo, score) values
(1, '001', 90),
(1, '002', 75),
(2, '002', 98),
(3, '001', 86),
(3, '003', 80),
(4, '004', 79),
(5, '005', 96),
(6, '006', 80);/* 地域表 */create table areas (id int primary key,name varchar(20),pid int
);insert into areas values
('130000', '河北省', null),
('130100', '石家庄市', '130000'),
('130400', '邯郸市', '130000'),
('130600', '保定市', '130000'),
('130700', '张家口市', '130000'),
('130800', '承德市', '130000'),
('440000', '广东省', null),
('440300', '深圳市', '440000'),
('440100', '广州市', '440000'),
('440500', '汕头市', '440000'),
('440600', '佛山市', '440000'),
('440200', '韶光市', '440000');create table departments (deptid int(10) primary key, deptname varchar(20) not null
);insert into departments values ('1001','市场部');
insert into departments values ('1002','测试部');
insert into departments values ('1003','开发部');create table employees (empid int primary key,empname varchar(20) not null,sex varchar(4) default null,deptid int default null,jobs varchar(20) default null,politicalstatus varchar(20) default null,leader int default null);insert into employees values ('1', '王昭君', '女', '1003', '开发', '群众', '9');
insert into employees values ('2', '诸葛亮', '男', '1003', '开发', '群众', '9');
insert into employees values ('3', '张飞', '男', '1002', '测试', '团员', '4');
insert into employees values ('4', '白起', '男', '1002', '测试经理', '党员', null);
insert into employees values ('5', '大乔', '女', '1002', '测试', '党员', '4');
insert into employees values ('6', '孙尚香', '女', '1001','市场', '党员', '12');
insert into employees values ('7', '百里玄策', '男', '1001', '市场', '团员', '12');
insert into employees values ('8', '小乔', '女', '1002', '测试', '群众', '4');
insert into employees values ('9', '百里守约', '男', '1003', '开发经理', '党员', null);
insert into employees values ('10', '妲己', '女', '1003', '开发', '团员', '9');
insert into employees values ('11', '李自', '男', '1002', '测试', '团员', '4');
insert into employees values ('12', '孙膑', '男', '1001', '市场经理', '党员', null);
insert into employees values ('13', '周瑜', '男', '1001', '市场', '群众', '12');create table salary(sid int primary key,empid int not null, salary int not null
);insert into salary values ('1', '7', '2100');insert into salary values ('2', '6', '2000');insert into salary values ('3', '12', '5000');insert into salary values ('4', '9', '1999');insert into salary values ('5', '10', '1900');insert into salary values ('6', '1', '3000');insert into salary values ('7', '2', '5500');insert into salary values ('8', '5', '2000');insert into salary values ('9', '3', '1500');insert into salary values ('10', '8', '4000');insert into salary values ('11', '11', '2600');insert into salary values ('12', '4', '5300');
24、连接查询介绍
24.1 内连接
-- sql 表a和表b数据
create table a(id int,name VARCHAR(20)
)create table b(id int,score int
)insert into a VALUES(1,'张三'),
(2,'李四'),(3,'王五')insert into b VALUES(1,80),
(2,88),(3,93),(4,99)
表a和表b内连接的查询:
select * from a INNER JOIN b on a.id = b.id;
查询结果:
24.1.1 练习
students表
scores表
查询结果
-- sql查询语句
select * from students INNER JOIN scores on students.studentNo = scores.studentNo;
24.1.2 隐式内连接
select * from students ,scores where students.studentNo = scores.studentNo;
24.1.3 内连接指定显示部分字段
select name,courseNo,score from students INNER JOIN scores on
students.studentNo = scores.studentNo;
24.1.4 显示表的别名
select name,courseNo,score from students st INNER JOIN scores sc on
st.studentNo = sc.studentNo;
24.1.5 带有where的内连接
courses表
-- 查询王昭君 的信息,要求只显示姓名、课程号、成绩。
select name,courseNo,score from students st INNER JOIN scores sc
on st.studentNo = sc.studentNo where st.name = '王昭君';
-- 查询姓名为“王昭君”,并且成绩小于90 的信息,要求只显示姓名、成绩。
select name,score from students stu INNER JOIN scores sc ON
stu.studentNo = sc.studentNo where stu.name='王昭君' and score < 90;
24.1.6 多表内连查询
-- 查询学生信息和成绩以及成绩对应的课程名称select name,coursename,score from students st INNER JOIN scores s on
st.studentNo = s.studentNo INNER JOIN courses c on c.courseNo = s.courseNo;
select name,score,courseName from students st INNER JOIN scores s ON
st.studentNo = s.studentNo INNER JOIN courses c on s.courseNo = c.courseNo
where courseName = 'linux';
-- 查询 成绩最高的男生信息,要求显示姓名、课程名、成绩。
select name,score,courseName from students st INNER JOIN scores s ON
st.studentNo = s.studentNo INNER JOIN courses c on s.courseNo = c.courseNo
where sex='男'
ORDER BY score DESC LIMIT 1;
24.2 左连接
-- 查询所有学生的信息以及成绩,包括没有成绩的学生。
select * from students st LEFT JOIN scores s ON
st.studentNo = s.studentNo;
24.3 右连接
-- 查询所有课程信息,包括没有成绩的。
select * from scores s right JOIN courses c ON
c.courseNo = s.courseNo;
撒花,今天学到这里吧,明天早上学完mysql剩下的,然后开始学习python部分啦~。