一、首先需要创建一个名为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;
select distinct part_id from worker;
select count(work_id) as '职工人数' from worker;
select min(salary) as '最低工资',max(salary) as '最高工资' from worker;
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';
select work_id,name,birth_date from worker where name like '刘%';
select name,work_time from worker where birth_date < '1960-1-1';
select name from worker where salary>=1000 and salary<=2000;
select name from worker where name like '陈%' or name like '李%';
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;
select work_id,name,salary from worker order by salary desc limit 3;
select part_id as '部门',count(politics_status) as '党员人数' from worker groupp by part_id;
select part_id as '部门',avg(salary) as '平均工资',sum(salary) as '总工资' fromm worker group by part_id;
select part_id as '部门',count(work_id) as '人数' from worker group by part_id having count(work_id)>=4;
表中数据参考: