MySQL:习题
建库
create database oldboy charset utf8;
建表
create table employee(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int);
插值
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'20170301','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部
('alex','male',78,'20150302','teacher',1000000.31,401,1),
('wupeiqi','male',81,'20130305','teacher',8300,401,1),
('yuanhao','male',73,'20140701','teacher',3500,401,1),
('liwenzhou','male',28,'20121101','teacher',2100,401,1),
('jingliyang','female',18,'20110211','teacher',9000,401,1),
('jinxin','male',18,'19000301','teacher',30000,401,1),
('成龙','male',48,'20101111','teacher',10000,401,1),
('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),
('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3);
简单查询
select id,name,sex,age,hire_date,post,post_comment,salary,office,depart_id from employee;
select * from employee;
desc employee; #查看表结构
通过四则运算查询
select name,salary*12 from employee; #
select name,salasy*12 as annual_salary from employee;
select name,salasy*12 annual_salary from employee;
定义显示格式
concat()函数用与连接字符串
select concat('姓名:',name,'年薪:',salary*12) as annual_salary from employee;
1 查出所有员工的名字,薪资,格式为<姓名:egon>,<薪资:3000>
select concat('< 姓名:',name,'>', '<薪资:',salary,'>')from employee;
2 查出所有的岗位(去掉重复)
select distinct post from employee;
3 查出所有员工名字,以及他们呢的年薪,年薪的字段名为annual_year
select name,salary*12 annual_salary from employee;
4 单条件查询
select name from employee where post = 'sale';
5 多条件查询
select name,salary from employee where post ='teacher'and salary>10000;
6 关键字between and (范围)
select name,salary from employee
where salary between 10000 and 20000; #薪资在10000~20000的范围
7 关键字is null(判断某个字段是否未null不能用等号,需要用is)
select name,post_comment from employee
where salary between 10000 and 20000;
select name,post_comment from employee
where salary not between 10000 and 20000;
select name,post_comment from employee
where post_comment = ''; #注意''是空字符,不是null
8 关键字in集合查询(查询出薪资为3000、3500、4000、9000的姓名和薪资)
select name,salary from employee
where salary = 3000 or salary = 3500 or salary = 4000 or salary = 9000;#传统方法
select name,salary from employee
where salary in(3000,3500,4000,9000); #使用in方法查询
select name,salary from employee
where salary not in(3000,3500,4000,9000); #not in查询salary不包含括号内的字段
9 关键字like模糊查询
通配符'%'查询名字带有eg的员工
select * from employee where name like 'eg%';
通配符'_'查询名字带有al的员工
select * from employee where name like 'al__';
10 查看岗位是teacher的员工姓名、年龄
select name,age,post from employee where post = 'teacher';
11 查看岗位是teacher且年龄大于30岁的员工姓名、年龄
select name,age,post from employee where post ='teacher' and age>30;
12 查看岗位是teacher且薪资在9000-10000范围内的员工姓名、年龄、薪资
select name,age,post,salary from employee where post='teacher' and salary between 9000 and 10000;
13 查看岗位描述不为null的员工信息
select * from employee where post_comment is not null;
14 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资
select name,age,salary,post from employee where post='teacher' and salary in(10000,9000,30000);
15 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资
select name,age,salary,post from employee where post='teacher' and salary not in(10000,90000,30000);
16 查看岗位是teacher且名字是jin开头的员工姓名、年薪
select name,salary*12 annual_year from employee where post='teacher' and name like 'jin%';
17 查询岗位名以及岗位包含的所有员工名字
select post,group_concat(name) from employee group by post;
18 查询岗位名以及各岗位内包含的员工个数
select post,count(id) from employee group by post;
19 查询公司内男员工和女员工的个数
select sex,count(id) from employee group by sex;
20 查询岗位名以及各岗位的平均薪资
select post,avg(salary) from employee group by post;
21 查询岗位名以及各岗位的最高薪资
select post,max(salary) from employee group by post;
22 查询岗位名以及各岗位的最低薪资
select post,min(salary) from employee group by post;
23 查询男员工与女员工的平均薪资
select sex,avg(salary) from employee group by sex;
24 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
select post,group_concat(name),count(id) from employee group by post having count(id) < 2;
25 查询各岗位平均薪资大于10000的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary) > 10000;
26 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) <20000;
27 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
select * from employee ORDER BY age asc,hire_date desc;
28 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列
select post,avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary) asc;
29 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) desc;
30 分页显示,每页5条
select * from employee limit 0,5;
select * from employee limit 5,5;
select * from employee limit 10,5;