练习1
1. 显示所有职工的基本信息
2. 查询所有职工所属部门的部门号,不显示重复的部门号
3. 求出所有职工的人数
4. 列出最高工资和最低工资
5. 列出职工的平均工资和总工资
6. 创建一个只有职工号、姓名和参加工作时间的新表,名为 工作日期表
8. 列出所有姓刘的职工和职工号、姓名和出生日期
9. 列出1960年以前出生的职工姓名、参加工作日期
10. 列出工资在1000-2000之间的所有职工姓名
11. 列出所有陈姓和李姓的职工姓名
12. 列出所有部门号为2和3的职工号、姓名、党员
13. 将职工表 worker 中的职工按出生的先后顺序排列
14. 显示工资最高的前3名职工的职工号和姓名
15. 求出各部门党员的人数
16. 统计各部门的工资和平均工资
17. 列出总人数大于4的部门号和总人数
第一步: 登录 mysql 数据库, 切换到 已创建的数据库中,然后创建 `worker` 表格
create table `worker` (
`部门号` int(11) not null,
`职工号` int(11) not null,
`工作时间` date not null,
`工资` float(8,2) not null,
`政治面貌` varchar(10) not null default '群众',
`姓名` varchar(20) not null,
`出生日期` date not null,
primary key (`职工号`)
) engine=InnoDB default charset=tuf8 row_format=dynamic;
insert into `worker` (`部门号` ,`职工号` , `工作时间` , `工资` , `政治面貌` , `姓名` , `出生日期` ) values (101 , 1001 , '2015-5-4' , 3500.00 , '群众' , '张三' , '1990-7-1');
insert into `worker` (`部门号` ,`职工号` , `工作时间` , `工资` , `政治面貌` , `姓名` , `出生日期` ) values (101 , 1002 , '2017-2-6' , 3200.00 , '团员' , '李四' , '1997-2-8');
insert into `worker` (`部门号` ,`职工号` , `工作时间` , `工资` , `政治面貌` , `姓名` , `出生日期` ) values (102 , 1003 , '2011-1-4' , 8500.00 , '党员' , '王亮' , '1983-6-8');
insert into `worker` (`部门号` ,`职工号` , `工作时间` , `工资` , `政治面貌` , `姓名` , `出生日期` ) values (102 , 1004 , '2016-10-10' , 5500.00 , '群众' , '赵六' , '1994-9-5');
insert into `worker` (`部门号` ,`职工号` , `工作时间` , `工资` , `政治面貌` , `姓名` , `出生日期` ) values (102 , 1005 , '2014-4-1' , 4800.00 , '党员' , '钱七' , '1992-12-30');
insert into `worker` (`部门号` ,`职工号` , `工作时间` , `工资` , `政治面貌` , `姓名` , `出生日期` ) values (102 , 1006 , '2017-5-5' , 4500.00 , '党员' , '孙八' , '1996-9-2');
(1)显示所有职工的基本信息
mysql> select *from `worker`;
(2)查询所有职工所属部门的部门号,不显示重复的部门号
mysql> select distinct `部门号` from `worker`;
(3)求出所有职工的人数
mysql> select count(*) from `worker`;
(4)列出最高工资和最低工资
mysql> select max(`工资`) as '最高工资',min(`工资`) as '最低工资' from `worker`;
(5)列出职工的平均工资和总工资
mysql> select avg(`工资`),sum(`工资`) from `worker`;
(6)创建一个只有职工号、姓名和参加工作时间的新表,名为 工作日期表
mysql> create table `work_date` select `职工号`,`姓名`,`工作时间` from`worker`;
mysql> select *from `work_date`;
(8) 列出所有姓刘的职工和职工号、姓名和出生日期
mysql> select `职工号`,`姓名`,`出生日期` from `worker` where `姓名`='刘%';
或者 mysql> select `职工号`,`姓名`,`出生日期` from `worker` where `姓名` like '刘%';
(9)列出1960年以前出生的职工姓名、参加工作日期
mysql> select `姓名`,`工作时间` from `worker` where `出生日期` < '1960-1-1';
(10)列出工资在1000-2000之间的所有职工姓名
`worker` 表中没有工资在 1000-2000 之间的,需要插入数据
mysql> insert into `worker` values(102,1007,'2014-5-2',1000.00,'群众','Amy','2000-2-1');
mysql> insert into `worker` values(103,1008,'2015-5-2',2000.00,'党员','BBob','1994-2-1');
mysql> insert into `worker` values(103,1009,'2016-3-2',1500.00,'群众','ccici','1992-4-1');
mysql> insert into `worker` values(103,1010,'2017-3-2',1950.00,'党员','David','1990-4-5');
查询:
mysql> select `姓名` from `worker` where `工资` >= 1000.00 and `工资`<= 2000.00 ;
或者 mysql> select `姓名` from `worker` where `工资` between 1000.00 and 2000.00 ;
(11)列出所有陈姓和李姓的职工姓名
mysql> select `姓名` from `worker` where `姓名` like '陈%' or `姓名` like '李%';
(12)列出所有部门号为2和3的职工号、姓名、党员
mysql> select `职工号`,`姓名`,`政治面貌` from `worker` where `部门号` in (102,103) and `政治面貌`='党员';
(13)将职工表 worker 中的职工按出生的先后顺序排列
mysql> select * from `worker` order by `出生日期`;
(14)显示工资最高的前3名职工的职工号和姓名
mysql> select `职工号`,`姓名` from `worker` order by `工资` desc limit 0,3;
或者 mysql> select `职工号`,`姓名` from `worker` order by `工资` desc limit 3;
(15)求出各部门党员的人数
mysql> select `部门号`,count(*) from `worker` where `政治面貌`='党员' group by `部门号`;
(16) 统计各部门的工资和平均工资
mysql> select `部门号`,sum(`工资`),avg(`工资`) from `worker` group by `部门号`;
(17) 列出总人数大于4的部门号和总人数
mysql> select `部门号`,count(8) from `worker` group by `部门号` having count(*) > 4;