插入语句
插入语句说那个关键字 insert into
语句 : insert into 表名(字段1,字段2……) values(值1,值2)
数据库插入语句 不需要按照表中的字段顺序进行填写字段 但是值和字段必须一一对应
也不要求将所有字段进行数据插入
insert into student(username,age,province,sex,education,t_no) values('庞广达',32,'湖北',1,'本科',10001);
修改语句
修改就是直接修改掉数据库中已存在的数据 使用关键字 update
语句 : update 表名 set 字段1= 值1,字段2 = 值2…… where 条件
where后面跟上的是 修改的条件 也就是修改的某一条数据
一般情况下我们以ID(主键)作为添加 因为不会重复 是唯一的
update student set username = '毛毛虫',age = 16 where id = 20210304
删除语句
删除语句 使用的相对来说比较少
语句 : delete from 表名 where 条件
delete from student where id = 20210304
查询语句
在数据库操作中 最重要的就是查询语句 在一个项目中 查询语句能占有整体SQL语句中的60%以上
所以说 我们最多的就是使用查询语句
语句 : select * from 表名 || select 字段1,字段2…… from 表名
-- 使用select最简单的查询语句
select * from student
1.where子句
以上查询是将整个数据表中所有的内容全部查询完毕,但是这样效果并不是特别的好
因为很多时候 我们查询语句的时候 都是查询部分数据 或者是查询出一条数据
我们多用条件查询 条件和修改删除语句一样 都是使用where进行连接条件
where子句中有很多中查询语句 我们主要是运算符查询 但是我们要查询的内容比较多
语句 : select * from 表名 where 条件
1.算数运算符查询
算数运算符一般情况下就是 + - * / %
-- where子句查询
-- 算数运算符查询
select * from student where age = 15 + 3;
select * from student where age = 20 - 3;
select * from student where age = 3 * 7;
select * from student where age = 40 / 2;
select * from student where sex % 2 = 0;
2.比较运算符查询
比较运算符一般是 > >= < <= != <> =
-- 比较运算符
select * from student where age > 20;
select * from student where age >= 20;
select * from student where age < 20;
select * from student where age <= 20;
select * from student where age != 20;
select * from student where age <> 20;
3.逻辑运算符查询
逻辑运算符就是 逻辑与and 逻辑或or
-- 逻辑运算符查询
select * from student where age > 20 and sex = 0;
select * from student where age < 20 or sex = 0;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ojc9tPxV-1616588687015)(C:\Users\Administrator\Desktop\MySQL录课课件\image\select5.png)]
2.聚合查询
聚合查询其实就是一种运算 他能运算出一个字段中所有数据的总量 平均值 最大值 最小值 总和等
其实就是在计算一个字段中的数据量 这个计算一般情况下 我们都会选择整形的字段
总量(count) 最大值(max) 最小值(min) 平均值(avg) 总和(sum)
语句 : select 聚合运算类型(字段名) from 表名
-- 总量
select count(id) from student;
select count(username) from student;
select count(*) from student;
-- 最大值
select max(age) from student;
-- 最小值
select min(age) from student;
-- 平均值
select avg(age) from student;
-- 总和
select sum(age) from student;
3.分组查询
分组查询其实就是将数据进行分组
分组查询关键字是group by 但是注意 分组查询中不能出现where语句
语句 : select 字段 from 表名 group by 分组的字段
-- 分组查询
select sex,count(id) from student group by sex;
select t_no,count(id) from student group by t_no;
4.排序查询
其实排序查询应用最为广泛 因为我们很多的数据几乎都是逆序排序查找出来的
朋友圈中的数据 QQ邮箱的邮件 新闻网站 这种查询几乎都是逆序查找出来的
注意 排序查询和分组查询一样 都是不能使用where的查询方式 排序的字段一般情况下都是整型
排序查询的关键字 order by
排序方式有两种 asc 顺序(默认) desc 逆序
语句 : select * from 表名 order by 排序的字段 desc || asc
-- 排序查询
select * from student order by age desc
select * from student order by age asc
5.受限查询
受限查询一般用于分叶管理 我们使用的时候 很多时候都会和排序 或者是分组嵌套使用
受限查询使用关键字 : limit
后面跟上两个参数 第一个参数x 受限制的起始位置 第二个参数是y 数据的步长 查找出来的数据量
注意 : 受限查询也不实用where
语句 : select * from 表名 limit x,y
-- 受限查询
select * from student limit 0,3;
select * from student limit 3,3;
select * from student order by age desc limit 0,3
6.区间查询
区间查询其实就是查询出一段区间中的数据 这个查询其实使用比较运算符也能完成
使用大于等于和小于等于 只不过区间查询更简单一些
区间查询关键字 : between
语句 : select * from 表名 where 字段 between x and y
-- 区间查询
select * from student where age >= 20 and age <= 26;
select * from student where age between 20 and 26;
7.多表查询
什么是多表查询 就是两个或者两个以上的多个表进行一起查询
我们想要完成多表查询 那么必须保证多个表之间有所关联
怎么关联两张表 一个表中的主键和另一个表中的非主键字段保持一致
多表查询关键字 : join on left join right join
使用join连接两个表
使用as给表取一个别名
语句 : select a.字段,b.字段 from a表名 join b表名 on 条件
-- 多表查询
-- 这种查询方式和之前不同 因为多表查询 所以说必须添加条件
select t.username,s.username,s.age,s.sex from teacher as t join student as s
-- 在join多表查询中 添加条件不是使用where 使用的on join和on进行连用
select t.username,s.username,s.age,s.sex from teacher as t join student as s on t.id = s.t_no
查询的结果
左,右联合查询
左联合查询就是在查询的时候以左表为主 提高查询速度 如果说左表中存在数据二右表中没有与之匹配的数据
那么 右表使用null进行补齐 左表中所有的数据全部显示出来
右联合同理
什么是左表 卸载join左边的就是左表
什么是右表 卸载join右边的就是右表
语句 : select a.字段,b.字段 from a表名 left || reght join b表名 on 条件
-- 左联合查询
select t.username,s.username,s.age,s.sex from teacher as t left join student as s on t.id = s.t_no
-- 右联合查询
select t.username,s.username,s.age,s.sex from teacher as t right join student as s on t.id = s.t_no
-- 确定左右表
select t.username,s.username,s.age,s.sex from student as s right join teacher as t on t.id = s.t_no
查询的结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eheYsEUq-1616588687025)(C:\Users\Administrator\Desktop\MySQL录课课件\image\select11.png)]