一、首先需要创建一个名为worker的表:
mysql> create table worker(
-> part_id int(11) not null comment '部门号',
-> work_id int(11) not null comment '职工号',
-> work_time date not null comment '工作时间',
-> salary float(8,2) not null comment '工资',
-> politics_status varchar(20) not null comment '政治面貌',
-> name varchar(20) not null comment '姓名',
-> birth_date date not null comment '出生日期',
-> primary key(work_id)
-> )engine=innodb default charset=utf8 row_format=dynamic;
Query OK, 0 rows affected, 4 warnings (0.27 sec)
二、插入数据:
insert into worker(part_id,work_id,work_time,salary,politics_status,name,birth_date) values(101,1001,'2015-5-4','3500.00','群众','张三','1990-7-1');
insert into worker(part_id,work_id,work_time,salary,politics_status,name,birth_date) values(101,1002,'2017-2-6','3200.00','团员','李四','1997-2-8');
insert into worker(part_id,work_id,work_time,salary,politics_status,name,birth_date) values(102,1003,'2011-1-4','8500.00','党员','王亮','1983-6-8');
insert into worker(part_id,work_id,work_time,salary,politics_status,name,birth_date) values(102,1004,'2016-10-10','5500.00','群众','赵六','1994-9-5');
insert into worker(part_id,work_id,work_time,salary,politics_status,name,birth_date) values(102,1005,'2014-4-1','4800.00','党员','钱七','1992-12-30');
insert into worker(part_id,work_id,work_time,salary,politics_status,name,birth_date) values(102,1006,'2017-5-5','4500.00','党员','孙八','1996-9-2');
三、查询
1、显示所有职工的基本信息。
select * from worker;
2、查询所有职工所属部门的部门号,不显示重复的部门号。
select distinct part_id from worker;
3、求出所有职工的人数。
select count(work_id) as '职工人数' from worker;
4、列出最高工和最低工资。
select min(salary) as '最低工资',max(salary) as '最高工资' from worker;
5、列出职工的平均工资和总工资。
select avg(salary) as '平均工资',sum(salary) as '职工工资总数' from worker;
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
create table work_time select work_id,name,work_time from worker;
7、显示所有女职工的年龄。
因为之前的表中没有职工的性别和年龄,所以这里增加一步给表增加两个字段:gender、age
mysql> alter table worker add gender char(1);
mysql> alter table worker add age int;
然后给表中这两列输入一些数据:男=‘M’,女=‘F’;
ysql> update worker set gender='M';
mysql> update worker set age=21;
mysql> update worker set age=18 where part_id=101;
mysql> update worker set gender='M' where work_id=1006;
mysql> update worker set gender='M' where work_id=1005;
mysql> update worker set gender='M' where work_id=1003;
显示所有女职工的年龄:
mysql> select age from worker where gender='F';
8、列出所有姓刘的职工的职工号、姓名和出生日期。
select work_id,name,birth_date from worker where name like '刘%';
9、列出1960年以前出生的职工的姓名、参加工作日期。
select name,work_time from worker where birth_date < '1960-1-1';
10、列出工资在1000-2000之间的所有职工姓名。
select name from worker where salary>=1000 and salary<=2000;
11、列出所有陈姓和李姓的职工姓名。
select name from worker where name like '陈%' or name like '李%';
12、列出所有部门号为2和3的职工号、姓名、党员否。
select work_id,name,politics_status from worker where part_id regexp '10[23]'
and politics_status!='党员';
13、将职工表worker中的职工按出生的先后顺序排序。
select * from worker order by birth_date;
14、显示工资最高的前3名职工的职工号和姓名。
select work_id,name,salary from worker order by salary desc limit 3;
15、求出各部门党员的人数。
select part_id as '部门',count(politics_status) as '党员人数' from worker groupp by part_id;
16、统计各部门的工资和平均工资
select part_id as '部门',avg(salary) as '平均工资',sum(salary) as '总工资' fromm worker group by part_id;
17、列出总人数大于等于4的部门号和总人数。
select part_id as '部门',count(work_id) as '人数' from worker group by part_id having count(work_id)>=4;
表中数据参考: