mysql数据结构表如下:希望能够好好的参考学习,每天学一点,就进步一点。
update练习
drop table if exists employee;
create table if not exists employee
(
id int,
name varchar(20),
sex varchar(10),
birthday date,
salary float,
resume text
);
insert into employee values (1,'zhangsan','male','1980-11-25',2000,'good body');
insert into employee values(2,"lisi","male","1980-04-25",1000,"good body");
insert into employee values(3,"xiaohong","female","1978-11-25",4000,"good girl");
insert into employee values(4,"xiaofen","female","1988-11-25",3500,"good girl");
insert into employee values(5,"小红","female","1988-11-25",3500,"good girl");
将所有员工薪水修改为5000元。
update employee set salary=5000;
将姓名为’zhangsan’的员工薪水修改为3000元。
update employee set salary=3000 where name='zhangsan';
将姓名为’lisi’的员工薪水修改为4000元,sex改为female。
update employee set salary=4000,sex='female' where name='lisi';
将xiaohong的薪水在原有基础上增加1000元。
update employee set salary=salary+1000 where name='xiaohong';
删除数据 delete
语法:delete from 表名 [where ]; 删除数据
truncate 表名; 删除所有的数据
* truncate 和 delete的区别:(这个在面试中常常被问到,这个是考察你的细心度)
* truncate删除数据,先删除整个表。再创建一个新的空的表。(效率)
* delete删除数据,一条一条删除的。(*****)
* 事物(insert update delete)
删除表中名称为’班长’的记录。
* delete from user where name='小红';
删除表中所有记录。
* delete from user;
使用truncate删除表中记录。
* truncate user;
select 练习 (这个请好好练习下,也许在下一次的面试笔试当中你就会遇到)。
create table student(
id int,
name varchar(20),
chinese float,
english float,
math float
);
insert into student(id,name,chinese,english,math) values(1,'张小明',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'李进',67,53,95);
insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'李一',88,98,92);
insert into student(id,name,chinese,english,math) values(5,'李来财',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'张进宝',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'黄蓉',75,65,30);
查询表中所有学生的信息。
select * from student;
查询表中所有学生的姓名和对应的英语成绩。
select name,english from student;
过滤表中重复数据。
select distinct english from student;
统计每个学生的总分。
select name,math+english+chinese from student;
在所有学生总分数上加10分特长分。
select name,math+10,english+10,chinese+10 from student;
使用别名表示学生分数。
select name,(math+english+chinese) as sum from student;
查询姓名为李一的学生成绩
select * from student where name='李一';
查询英语成绩大于90分的同学
select name,english from student where english > 90;
查询总分大于200分的所有同学
select name,math+english+chinese from student where (math+english+chinese) > 200;
常用的符号
> < <= >= = <>(不等于)
in(范围内取内容)
like -- 模糊查询 写法:like '张_或者%'; _和%区别:占位符。_只一个%可以有多个
%的写法 like '%张'; 结果XXX张
like '张%'; 结果张XXX
like '%张%'; 只要有张就行
is null -- 判断是否为null
and -- 并且
or -- 或者
not -- 不成立
查询英语分数在 80-90之间的同学。
select * from student where english >80 and english <90;
select * from student where english between 80 and 90;
查询数学分数为89,90,91的同学。
select * from student where math in(89,90,91);
查询所有姓李的学生英语成绩。
select * from student where name like '李%';
查询数学分>80并且语文分>80的同学。
select * from student where math >80 and chinese > 80;
查询数学分>80或者语文分>80的同学。
select * from student where math >80 or chinese > 80;
查询英语>80或者总分>200的同学
select * from student where english>80 or (math+english+chinese) > 200;
排序 使用order by 升序默认的(asc)/降序(desc)
* 出现select的语句末尾。
对数学成绩排序后输出。
select name,math from student order by math;
对总分排序后输出,然后再按从高到低的顺序输出
select name,math+english+chinese from student order by (math+english+chinese) desc;
对学生成绩按照英语进行降序排序,英语相同学员按照数学降序
select * from student order by english desc,math desc;
对姓李的学生成绩排序输出
select * from student where name like '李%' order by english desc;
聚集函数
* count 获取数量
练习:
统计一个班级共有多少学生?
select count(*) from student;
统计数学成绩大于90的学生有多少个?
select count(*) from student where math >90;
统计总分大于150的人数有多少?
select count(*) from student where (math+english+chinese)>150;
* sum 求和(忽略null值) 可以同ifnull(xxx,0)
统计一个班级数学总成绩?
select sum(math) from student;
统计一个班级语文、英语、数学各科的总成绩
select sum(math),sum(english),sum(chinese) from student;
统计一个班级语文、英语、数学的成绩总和
select sum(ifnull(math,0)+english+chinese) from student;
select sum(math)+sum(english)+sum(chinese) from student;
统计一个班级语文成绩平均分
select sum(chinese)/count(*) from student;
* avg 平均数
练习:
求一个班级数学平均分?
select avg(math) from student;
求一个班级总分平均分
select avg(ifnull(math,0)+english+chinese) from student;
* max 最大值
select max(math) from student;
* min 最小值
select min(math) from student;
create table orders(
id int,
product varchar(20),
price float
);
insert into orders(id,product,price) values(1,'电视',900);
insert into orders(id,product,price) values(2,'洗衣机',100);
insert into orders(id,product,price) values(3,'洗衣粉',90);
insert into orders(id,product,price) values(4,'桔子',9);
insert into orders(id,product,price) values(5,'洗衣粉',90);
insert into orders(id,product,price) values(6,'电视',900);
* group by 分组(一起使用) 条件过滤需要是having,不能使用where
练习:对订单表中商品归类后,显示每一类商品的总价.
select product,count(*),sum(price)from orders group by product;
练习:查询购买了几类商品,并且每类总价大于100的商品
select product,sum(price) from orders group by product having sum(price)>100;
* 小结 select 语句 : S-F-W-G-H-O 组合 select ... from ... where ... group by... having... order by ... ;
顺序不能改变