【MySQL】增删改查操作 —— CRUD
【MySQL】增删改查操作 —— CRUD
- 0、前言
- 一、Create 新增
- 1.1 语法
- 1.2 举例
- 二、Retrieve 检索
- 2.1 select 查询
- 2.1.1 全列查询
- 2.1.2 指定列查询
- 2.1.3 查询字段为表达式
- 1. 常量表达式
- 2. 把所有学生的语文成绩加10分
- 3. 计算所有学生语文、数学和英语成绩的总分,并指定别名
- 4. 结果去重查询
- 2.2 Where 条件查询
- 2.2.1 语法
- 2.2.2 比较运算符
- 2.2.3 逻辑运算符
- 2.2.4 Where 查询练习
- 1. 基本查询
- 2. AND和OR
- 3. 范围查询
- 4. 模糊查询
- 5. NULL的查询
- 2.3 Order by 排序(对查询出来的结果进行排序)
- 2.4 分页查询
- 2.4.1 分页查询 语法
- 2.4.2 分页查询 实例
- (1)查看前3条记录
- (2)按照 id降序查看前2行
- (3)从0开始,向后查2条记录
- (4)limit num offset start;
- (5)当查询条数 大于 数据个数时:
- 三、Update 修改
- 3.1 语法
- 3.2 举例
- 3.2.1 update exam set math = 80 where name = '孙悟空';
- 3.2.2 update exam set math = 60,chinese = 70 where name = '曹孟德';
- 3.2.3 update exam set english = english+10 where name = '唐三藏';
- 3.2.4 update exam set math = math+30 oder by math+chinese+english asc limit 3;
- 3.2.5 updata exam set chinese = chinese * 2;
- 四、Delete 删除
- 4.1 语法
- 4.2 举例
- 4.2.1 delete from exam where name = '孙悟空';
- 五、截断表 —— 把表恢复到刚创建的状态
- 5.1 语法
- 5.2 举例
- 六、插入查询结果
- 6.1 语法
- 6.2 举例
- 七、聚合函数
- 7.1 常用函数
- 7.2 举例
- 7.2.1 COUNT
- 7.2.2 SUM
- 7.2.3 AVG
- 7.2.4 MAX
- 7.2.5 MIN
- 八、Group by 分组查询
- 8.1 语法
- 8.2 举例
- 8.3 having子句
- 九、内置函数
0、前言
一、Create 新增
1.1 语法
insert into table_name [(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value, [, value] ...
1.2 举例
DROP TABLE exam;-- 创建表结构
CREATE TABLE exam (id BIGINT,name VARCHAR(20) COMMENT '同学姓名',chinese float COMMENT '语文成绩',math float COMMENT '数学成绩',english float COMMENT '英语成绩'
);-- 插入测试数据
INSERT INTO exam (id,name, chinese, math, english) VALUES
(1, '唐三藏', 67, 98, 56),
(2, '孙悟空', 87, 78, 77),
(3, '猪悟能', 88, 98, 90),
(4, '曹孟德', 82, 84, 67),
(5, '刘旋得', 55, 85, 45),
(6, '孙权', 70, 73, 78),
(7, '宋公明', 75, 65, 30);-- 查询
select * from exam;
二、Retrieve 检索
2.1 select 查询
2.1.1 全列查询
sql select * from 表名;
表示在一个表中查询所有的记录。
举例:
DROP TABLE exam;-- 创建表结构
CREATE TABLE exam (id BIGINT,name VARCHAR(20) COMMENT '同学姓名',chinese float COMMENT '语文成绩',math float COMMENT '数学成绩',english float COMMENT '英语成绩'
);-- 插入测试数据
INSERT INTO exam (id,name, chinese, math, english) VALUES
(1, '唐三藏', 67, 98, 56),
(2, '孙悟空', 87, 78, 77),
(3, '猪悟能', 88, 98, 90),
(4, '曹孟德', 82, 84, 67),
(5, '刘旋得', 55, 85, 45),
(6, '孙权', 70, 73, 78),
(7, '宋公明', 75, 65, 30);-- 全列查询
select * from exam;
2.1.2 指定列查询
-- 指定列查询
select id,name,chinese from exam;
2.1.3 查询字段为表达式
1. 常量表达式
-- 查询字段为表达式
select id,name,chinese+math+english as sum from exam;
`
2. 把所有学生的语文成绩加10分
-- 把所有学生的语文成绩加10分
select id,name,chinese,chinese + 10 from exam;
3. 计算所有学生语文、数学和英语成绩的总分,并指定别名
-- 计算所有学生语文、数学和英语成绩的总分,并指定别名
select id,name,chinese+math+english as total from exam;
4. 结果去重查询
select math from exam;
-- 结果去重查询
select distinct math from exam;
注意:
2.2 Where 条件查询
2.2.1 语法
2.2.2 比较运算符
举例:
(1)查询字符编码集
(2)查询 exam表中姓名为孙的信息(模糊查询)
-- 查询 exam表中姓名为孙的信息
select * from exam where name like '孙%';
(3)查询 exam表中姓名为孙_的信息
select * from exam where name like '孙_';
(4)查询 exam表中姓名为孙__的信息
select * from exam where name like '孙__';
2.2.3 逻辑运算符
2.2.4 Where 查询练习
1. 基本查询
-- where 查询举例
select name , english from exam where english < 60;
select name, chinese,english from exam where chinese > english;
select name,chinese+math+english total from exam where chinese+math+english < 200;
注意:不可以使用别名在where进行比较!
2. AND和OR
select name, chinese, english from exam where chinese > 80 and english > 80;
select name, chinese, english from exam where chinese > 80 or english > 80;
3. 范围查询
select name,chinese from exam where chinese >= 80 and chinese <= 90;
或者
select name, math from exam where math in (78,79,98,99);
或者
&&&&&&&&&&&&&&
4. 模糊查询
5. NULL的查询
- 查询值为NULL的数据行
-- 插入张飞的信息
insert into exam values (8,'张飞',27,0,NULL);select * from exam where english is null;
或者
- 查询值不为NULL的数据行
select * from exam where english is not null;
- NULL与其他值进⾏运算结果为NULL
2.3 Order by 排序(对查询出来的结果进行排序)
-
语法
-
举例
select * from exam order by math asc;
update exam set math = 98,english = 20,where id = 8;
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&
[总结]
2.4 分页查询
2.4.1 分页查询 语法
2.4.2 分页查询 实例
(1)查看前3条记录
(2)按照 id降序查看前2行
(3)从0开始,向后查2条记录
(4)limit num offset start;
(5)当查询条数 大于 数据个数时:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&
三、Update 修改
3.1 语法
3.2 举例
3.2.1 update exam set math = 80 where name = ‘孙悟空’;
注意:
3.2.2 update exam set math = 60,chinese = 70 where name = ‘曹孟德’;
注意:
3.2.3 update exam set english = english+10 where name = ‘唐三藏’;
3.2.4 update exam set math = math+30 oder by math+chinese+english asc limit 3;
&&&&&&&&&&&&&&&&&&&&&&&
3.2.5 updata exam set chinese = chinese * 2;
四、Delete 删除
4.1 语法
4.2 举例
4.2.1 delete from exam where name = ‘孙悟空’;
&&&&&&&&&&&&&&&&&&&&&&
五、截断表 —— 把表恢复到刚创建的状态
5.1 语法
5.2 举例
&&&&&&&&&&&&&&&&&
六、插入查询结果
6.1 语法
6.2 举例
七、聚合函数
7.1 常用函数
7.2 举例
7.2.1 COUNT
7.2.2 SUM
7.2.3 AVG
7.2.4 MAX
7.2.5 MIN
八、Group by 分组查询
8.1 语法
8.2 举例
&&&&&&&&&&&&&&&&&&
8.3 having子句