MySQL-day2_01
学习目标:
1.掌握 where 子句;
2.掌握数据排序;
3.掌握五种聚合函数使用方法;
4.了解分组和分页。
(笔记会分两次写)
MySQL
- (一)数据准备
- (二)别名与重复记录
- (三)条件查询
- 一、where子句
- 二、比较运算符
- 三、逻辑运算符
- and(与):两个条件必须同时满足
- or(或):两个条件只要有一个满足即可
- not(非)
- 四、模糊查询
- 五、范围查询
- 六、空判断
- 七、where子句在 update 与 delete 语句中同样有效
(一)数据准备
创建学生表 students,向表中插入数据。
-- 如果学生表students存在,就删除学生表students
drop table if exists students;-- 创建学生表students
CREATE table students(studentNo varchar(10) primary key, -- 学号,主键,值不能重复name varchar(10), -- 姓名sex varchar(1), -- 性别hometown varchar(10), -- 家乡age tinyint, -- 年龄class varchar(10), -- 班级card varchar(20) -- 身份证号
);-- 向学生表 students 插入数据
insert into students values
('001','王昭君','女','北京',30,'1班','110101199003157654'),
('002','诸葛亮','男','上海',29,'2班','310102199104262354'),
('003','张飞','男','南京',30,'3班','320102199003047654'),
('004','白起','男','安徽',35,'4班','340202198505177654'),
('005','大乔','女','天津',28,'3班','120101199204067654'),
('006','孙尚香','女','河北',25,'1班','1305021995061377654'),
('007','百里玄策','男','山西',39,'2班','140102198107277654'),
('008','小乔','女','河南',25,'3班','null'),
('009','百里守约','男','湖南',31,'1班',''),
('010','妲己','女','广东',24,'2班','440701199607147654'),
('011','李白','男','北京',30,'4班','110202199005017745'),
('012','孙膑','男','新疆',36,'3班','650102198401297655')
点击“运行”,并刷新“表”。
(二)别名与重复记录
一、查询所有字段
例1:查询 students 表所有字段
select * from students;
二、查询指定字段
例1:查询 students 表 name,sex 和 age 字段
select name, sex, age from students;
三、字段的别名
在 select 后面的字段名部分,可以使用 as 为字段起别名,这个字段的别名出现在 SELECT 查询结果中。
例1:students 表的 name 字段别名为 姓名,sex 字段别名为 性别,age 字段别名为 年龄。
select name as 姓名, sex as 性别, age as 年龄 from students;
▲ as 可以省略。
select name 姓名, sex 性别, age 年龄 from students;
四、表的别名
在 from 后面的表名,可以使用 as 为表起别名。
例1:students 表的别名为 s。
select name, age from students as s;
as 可以省略。
例2:students 表的别名为 s。
select name, age from students s;
五、消除重复记录
在 select 后面字段前使用 distinct 可以消除重复的记录。
select distinct 字段 from 表名;
例1:查询 students 表的 sex 字段,用 distinct 取消重复记录。
select distinct sex from students;
(三)条件查询
一、where子句
使用 where 子句对表中的数据筛选,符号条件的数据会出现在结果集中。
select 字段1,字段2... from 表名 where 条件;
例1:查询 students 表中学号 studentNo 等于 ‘001’ 的记录
select * from students where studentNo='001';
例2:查询 students 表中年龄 age 等于 30 的姓名 name,班级 class
select name, class from students where age=30;
select 后面的 * 或字段名,决定了返回什么样的字段(列);
select 中 where 子句,决定了返回什么样的记录(行)
where 后面支持多种运算符,进行条件的处理。
⚪比较运算
⚪逻辑运算
⚪模糊查询
⚪范围查询
⚪空判断
二、比较运算符
- 等于:=
- 大于:>
- 大于等于:>=
- 小于:<
- 小于等于:<=
- 不等于:!= 或 <>
例1:查询 students 表中 name 等于 ‘小乔’ 学生的 age。
select age from students where name='小乔';
例2:查询 students 表中 30岁 以下的学生记录。
select * from students where age<30;
例3:查询 students 表中 hometown 不在 ‘北京’ 的学生记录。
select * from students where hometown!='北京';
三、逻辑运算符
and(与):两个条件必须同时满足
例:查询 age 年龄小于 30,并且 sex 为 ‘女’ 的同学记录
select * from students where age<30 and sex='女';
or(或):两个条件只要有一个满足即可
例:查询sex为’女’ 或者 class为’1班’的学生记录
select * from students where sex='女' or class='1班';
not(非)
⭐如果条件为满足,not 后变为不满足;
⭐如果条件为不满足,not 后变为满足。
例1:查询 hometown 非 ‘天津’ 的学生记录。
select * from students where not hometown='天津';
例2:查询 hometown 为 ‘天津’ 的学生记录。
select * from students where not hometown!='天津';
四、模糊查询
- like
- % 表示多个任意字符
- _ 表示一个任意字符
例1:查询 name 中以’孙’ 开头的学生记录
select * from students where name like '孙%';
例2:查询 name 以’孙’ 开头,且名只有一个字的学生记录
select * from students where name like '孙_';
例3:查询 name 为任意姓,名叫’乔’的学生记录
select * from students where name like '%乔';
例4:查询 name 中含有 ‘白’ 的学生记录
select * from students where name like '%白%';
五、范围查询
☆ in 表示在一个非连续的范围内
例1:查询 hometown 是 ‘北京’ 或 ‘上海’ 或 ‘广东’ 的学生记录
select * from students where hometown in('北京','上海','广东');
☆ between…and… 表示在一个连续的范围内
例2:查询 age 为 25 至 30 的学生记录
select * from students where age between 25 and 30;
六、空判断
注意:null与 ‘’ 是不同的
null:什么都没有
‘’ :长度为 0 的字符串
判断空:is null
例1:查询 card 身份证为 null 的学生记录
select * from students where card is null;
判断非空:is not null
例2:查询 card 非 null 的学生记录
select * from students where card is not null;
七、where子句在 update 与 delete 语句中同样有效
例1:修改 age 为 25,并且 name 为 ‘孙尚香’ 的学生 class 为 ‘2班’
update students set class='2班' where age=25 and name='孙尚香';
例2:删除 class 为 ‘1班’,并且 age 大于 30 的学生记录。
delete from students where age>30 and class='1班';
下一期见!
孩子干不动了/(ㄒoㄒ)/~~