MySQL 表的增删查改(练习)

简单增删查改

1.创建的商品表中插入一条数据:名称为“学生书包”、价格18.91、库存101、描述为空

insert into product(name,price,storage) values('学生书包',18.91,101);
insert into product values ('学生书包',18.91,101,null);

2.在图书表中新增一条记录:Java核心技术、作者“Cay S. Horstman”,价格56.43,分类为“计算机技术”

insert into book values('Java核心技术','Cay S.Horstman',56.43,'计算机技术');

3.删除商品表中,价格大于60,或者是库存小于200的记录

delete from product where price>60 or storage<200;

4.修改商品表中 所有库存大于30的商品记录,将价格增加50块

update product set price=price+50 where storage>30;

5.图书表中 修改“Java核心技术”的图书信息,将价格修改为61

update book set price=61 where name='Java核心技术';

6.student学生表中,字段有姓名name,年龄age,要求查询姓张,并且年龄在18到25岁之间的学生 

select * from student where name like '张%' and age between 18 and 25;

7.查询article文章表中,发表日期create_date在2019年1月1日上午10点30分至2019年11月10日下午4点2分的文章

select * from article where create_date between '2019-01-01 10:30:00' and '2019-11-10 16:02:00';

8.查询article文章表中,文章标题title为空,或者满足发表日期create_date在2019年1月1日之后

select * from article where title is null or create_date > '2019-01-01 00:00:00';

 9.查询book图书表中,作者author列不为空,或者满足条件:价格price在50元以上且出版日期publish_date在2019年之后的图书信息

select * from book where author is not null or (price >50 and publish_date>'2019-01-01 00:00:00');

10.

查询用户user表中,同时满足以下两个条件的用户数据:

1. ID在1至200或300至500,且账号accout列不为空

2. 充值金额amount在1000以上。

select * from user where (id between 1 and 200 or id between 300 and 500) and accout is not null and amount>1000;

聚合查询

有一张员工表emp,字段:姓名name,性别sex,部门depart,工资salary。查询以下数据:

1、查询男女员工的平均工资

select sex,avg(salary) from emp group by sex;

说明:平均值使用聚合函数avg,并且按照性别男女分组,group by 性别字段

2、查询各部门的总薪水

select depart,sum(salary) from emp group by depart;

说明:总薪水使用聚合函数sum取薪水字段求和,并且按照部门字段分组,group by 部门字段

3、查询总薪水排名第二的部门

select depart,sum(salary) from emp group by depart order by sum(salary) desc limit 1,1;

说明:order by语句先按照总薪水排序,之后取第二条数据,可以使用分页,每一页1条数据,第二页就是该结果

4、查询姓名重复的员工信息

select name from emp group by name having count(name)>1;

说明:名字重复,说明同一个名字有多条数据,可以先按照名字分组,分组之后再过滤行数大于1的,就表示同一个名字至少有2条记录,重复了

5、查询各部门薪水大于10000的男性员工的平均薪水

select depart,avg(salary) from emp where salary>10000 and sex='男' group by depart;

说明:这里需要注意题目要求是查询薪水大于10000的男性员工,这个是在按部门分组前就过滤,在过滤后的结果集中再查询各个部门的平均薪水

有员工表、部门表和薪资表,根据查询条件写出对应的sql【同程艺龙2020届校招笔试题】

现在有员工表、部门表和薪资表。

部门表depart的字段有depart_id, name;

员工表 staff 的字段有 staff_id, name, age, depart_id;

薪资表salary 的字段有 salary_id,staff_id,salary,month。

(问题a):求每个部门'2016-09'月份的部门薪水总额

select det.name ,sum(sal.salary) from salary sal
join staff sta on sal.staff_id = sta.staff.id
join depart dep on sta.depart_id = dep.depart_id
where year (sal.month) = 2016
and month (sal.month) = 9
group by dep.depart_id

(问题b):求每个部门的部门人数,要求输出部门名称和人数

select dep.name, count(sta.staff_id)

from staff sta

join depart dep on dep.depart_id = sta.staff_id

group by sta.depart_id

(问题c):求公司每个部门的月支出薪资数,要求输出月份和本月薪资总数

select dep.name,sal.month,sum(sal.salary)

from depart dep

join staff sta on dep.depart_id = sta.depart_id

join salary sal  on sta.staff_id = sal.staff_id

group by dep.depart_id ,sal.month;

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值