MySQL-2--数据库的查询
一、题目
二、数据库的建立和数据录入
数据库的建立
mysql> create database mydb8_worker;
Query OK, 1 row affected (0.01 sec)mysql> use mydb8_worker;
Database changed
表的建立
mysql> create table t_worker(-> department_id int(11) not null comment '部门号',-> worker_id int(11) primary key not null comment '职工号',-> worker_date date not null comment '工作时间',-> wages float(8,2) not null comment '工资',-> politics varchar(10) not null default '群众' comment '政治面貌',-> name varchar(20) not null comment '姓名',-> borth_date date not null comment '出生日期' );
Query OK, 0 rows affected, 3 warnings (0.04 sec)
数据的录入
mysql> insert into t_worker values (101, 1001, '2015-5-4',7500.00,'群众','张春燕','1990-7-1'),-> (101,1002,'2019-2-6',5200.00,'团员','李名博','1997-2-8'),-> (102,1003,'2008-1-4',10500.00,'党员','王博涵','1983-6-8'),-> (102,1004,'2016-10-10',5500.00,'群众','赵小军','1994-9-5'),-> (102,1005,'2014-4-1',8800.00,'党员','钱有财','1992-12-30'),-> (103,1006,'2019-5-5',5500.00,'党员','孙菲菲','1996-9-2');
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
三、数据查询
3.完成查询
(1)、显示所有职工的基本信息。
(2)、查询所有职工所属部门的部门号,不显示重复的部门号。
(3)、求出所有职工的人数。
(4)、列出最高工和最低工资。
(5)、列出职工的平均工资和总工资。
在t_worker表中,计算平均工资、总工资,最后输出。
(6)、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
1、创建工作日期表:
mysql> create table 工作日期表(-> worker_id int(11) primary key not null comment '职工号',-> name varchar(20) not null comment '姓名',-> worker_date date not null comment '工作时间');
Query OK, 0 rows affected, 1 warning (0.03 sec)
插入数据:
mysql> insert into 工作日期表 values(1001,'张春燕','2015-5-4'),-> (1002,'李名博','2019-2-6'),-> (1003,'王博涵','2008-1-4'),-> (1004,'赵小军','2016-10-10'),-> (1005,'钱有财','2014-4-1'),-> (1006,'孙菲菲','2019-5-5');
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
2、将查询到职工号、姓名和参加工作的数据放入建立的新表“工作日期表”。
mysql> create table 工作日期表 select worker_id, name, worker_date from t_worker;
Query OK, 6 rows affected (0.06 sec)
Records: 6 Duplicates: 0 Warnings: 0
(7)、显示所有党员的年龄。
在t_worker表中,先查找是党员的人,再计算党员年龄,最后输出。
(8)、列出工资在4000-8000之间的所有职工姓名
在t_worker表中,查找工资在4000-8000之间的所有职工,最后输出职工姓名。
(9)、列出所有孙姓和李姓的职工姓名。
在t_worker表中,先查找孙姓和李姓的人,最后输出。
(10)、列出所有部门号为102和103日不是党员的职工号、姓名。
在t_worker表中,查找部门为102和103的人,不是党员,最后输出。
(11)、将职工表t worker中的职工按出生的先后顺序排序。
在t_worker表中,按照职工的出生日期升序排列,最后输出。
(12)、显示工资最高的前3名职工的职工号和姓名。
在t_worker表中,先降序排列工资,再返回前3行的结果,最后输出。
(13)、求出各部门党员的人数。
在t_worker表中,先查找是党员的人,再按照部门号分组,count()求出各部门的党员人数,最后输出。
(14)、统计各部门的工资和平均工资并保留2位小数
在t_worker表中,先按照部门号分组,再求出各部门工资和,然后求出各部门的平均工资并用round保留2位小数,最后输出。
(15)列出总人数大于等于3的部门号和总人数。
在t_worker表中,先按照部门号分组,再count()求出各部门的党员人数,having 过滤出部门人数>=3的部门,最后输出总人数大于等于3的部门号和总人数。