(1)创建一个测试数据库
create databate text_3_22;(2)进入数据库
use text_3_22;(3)创建表并插入数据(我已经为你们准备好了,记得插入后再进行测试)
-- 创建表结构
create table exam_result (
id int unsigned primary key auto_increment,
name varchar(20) not null comment '同学姓名',
chinese float default 0.0 comment '语文成绩',
math float default 0.0 comment '数学成绩',
english float default 0.0 comment '英语成绩'
);
-- 插入测试数据
insert into exam_result (name, chinese, math, english) values
('唐三藏', 67, 98, 56),
('孙悟空', 87, 78, 77),
('猪悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55, 85, 45),
('孙权', 70, 73, 78),
('宋公明', 75, 65, 30);(4)记得检验自己插入的数据是否正确
select *from exam_result;
update exam_result set math=80 where name='孙悟空';更新前: | 更新后 |
|---|---|
| |

update exam_result set math=60,chinese=70
where name='曹孟德'; 更新前: | 更新后 |
|---|---|

update exam_result set math=math+30 order
by math+english+chinese asc limit 3;更改前后三名 | 更改后后三名: |
|---|---|

delete from exam_result where name='孙悟空';删除前 | 删除后 |
|---|---|

delete from exam_result order by english+math+chinese asc limit 1;删除前: | 删除后: |
|---|---|

CREATE TABLE for_delete (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20)
);
---准备一个测试表结构insert for_delete (name) values('a'),('b'),('c');
----准备测试数据方案一:删除表 | 方案二:截断表 |
|---|---|
delete from for_delete; | truncate for_delete; |
create table 1_distinct_text(
name varchar(20)
);
---创建测试表insert 1_distinct_text values
('aaa'),('bbb'),('aaa'),('bbb'),('aaa'),('bbb');创建相同的表结构 | 创建相同表结构并插入去重元素 |
|---|---|
create table <新表名> like <原表名>; | create table <新表名> as select distinct *from <旧表名>; |
在新表中插入去重数据 | |
insert <新表名> select distinct *from<原表名>; | |
删除旧表 | |
drop table <旧表>; | |
改新表名为旧表 | |
alter table <新表名> rename to <旧表名> | |

select count(math) from exam_result;统计所有数学成绩 | 统计不重复数学成绩 |
|---|---|

select sum(math) from exam_result;
方案一:
select sum(math)/count(math) from exam_result;方案二:
select avg (math) from exam_result;
select min(math) from exam_result where math>70;
注:准备数据导入到数据库里面(scott_data.sql)
导入操作在(Mysql的库操作-CSDN博客)中的数据恢复里面
案例:
select max(sal),avg(sal) from emp group by deptno;
select min(sal),avg(sal) from emp group by job;
select min(sal),avg(sal) from emp group by deptno,jop;
select deptno ,avg(sal) deptsal from emp group by deptno
having deptsal<2000;