MySQL数据库中对表进行创建,插入数据并对数据进行选择

 

目录

 1.根据此图进行建表并插入数据

 2.对表进行以下操作

a:显示所有职工的基本信息

b:查询所有职工所属部门的部门号,不显示重复的部门号

c:求出所有职工的人数

d:列出最高工和最低工资

e:列出职工的平均工资和总工资

f:创建一个只有职工号、姓名和参加工作的新表,名为工作日期表

g:列出1960年以前出生的职工的姓名、参加工作日期

h:列出工资在1000一2000之间的所有职工姓名

i:列出所有陈姓和李姓的职工姓名

j:列出所有部门号为102和103的职工号、姓名、党员否

k:将职工表worker中的职工按出生的先后顺序排序

l:显示工资最高的前3名职工的职工号和姓名

m:求出各部门党员的人数

n:统计各部门的工资和平均工资

o:列出总人数大于4的部门号和总人数


MySQL数据库中对表进行创建,插入数据并对数据进行选择_第1张图片

 1.根据此图进行建表并插入数据
#建表
mysql> create table 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 '出生日期'
    -> )engine=InnoDB default charset=utf8 row_format=dynamic;
#向表中插入数据
mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (101,1001,'2015-5-4',3500.00,'群众','张三','1990-7-1');
Query OK, 1 row affected (0.00 sec)

mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (101,1002,'2017-2-6',3200.00,'团员','李四','1997-2--8');
Query OK, 1 row affected (0.00 sec)

mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1003,'2011-1-4',8500.00,'党员','王亮','1983-6-8');
Query OK, 1 row affected (0.00 sec)

mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1004,'2016-10-10',5500.00,'群众','赵六','1994-9-5');
Query OK, 1 row affected (0.00 sec)

mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1005,'2014-4-1',4800.00,'党员','钱七','1992-12-30');
Query OK, 1 row affected (0.00 sec)

mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1006,'2017-5-5',4500.00,'党员','孙八','1996-92-2');
Query OK, 1 row affected (0.00 sec)
 2.对表进行以下操作
a:显示所有职工的基本信息
mysql> select *from worker;
+---------------+-----------+-------------+---------+----------+--------+------------+
| department_id | worker_id | worker_date | wages   | politics | name   | borth_date |
+---------------+-----------+-------------+---------+----------+--------+------------+
|           101 |      1001 | 2015-05-04  | 3500.00 | 群众     | 张三   | 1990-07-01 |
|           101 |      1002 | 2017-02-06  | 3200.00 | 团员     | 李四   | 1997-02-08 |
|           102 |      1003 | 2011-01-04  | 8500.00 | 党员     | 王亮   | 1983-06-08 |
|           102 |      1004 | 2016-10-10  | 5500.00 | 群众     | 赵六   | 1994-09-05 |
|           102 |      1005 | 2014-04-01  | 4800.00 | 党员     | 钱七   | 1992-12-30 |
|           102 |      1006 | 2017-05-05  | 4500.00 | 党员     | 孙八   | 1996-09-02 |
+---------------+-----------+-------------+---------+----------+--------+------------+
6 rows in set (0.00 sec)
b:查询所有职工所属部门的部门号,不显示重复的部门号
mysql> select distinct department_id from worker;
+---------------+
| department_id |
+---------------+
|           101 |
|           102 |
+---------------+
2 rows in set (0.01 sec)
c:求出所有职工的人数
mysql> select count(name) from worker;
+-------------+
| count(name) |
+-------------+
|           6 |
+-------------+
1 row in set (0.01 sec)
d:列出最高工和最低工资
mysql> select max(wages) as '最高工资',min(wages) as '最低工资' from worker; 
+--------------+--------------+
| 最高工资     | 最低工资     |
+--------------+--------------+
|      8500.00 |      3200.00 |
+--------------+--------------+
1 row in set (0.00 sec)

mysql> 
e:列出职工的平均工资和总工资
mysql> select avg(wages) as '平均工资',sum(wages) as '总工资' from worker;
+--------------+-----------+
| 平均工资     | 总工资    |
+--------------+-----------+
|  5000.000000 |  30000.00 |
+--------------+-----------+
1 row in set (0.00 sec)
f:创建一个只有职工号、姓名和参加工作的新表,名为工作日期表
mysql> create table workdate select worker_id,name from worker;
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0
g:列出1960年以前出生的职工的姓名、参加工作日期
mysql> select name,worker_date from worker where borth_date < '1960-1-1';
Empty set (0.00 sec)
h:列出工资在1000一2000之间的所有职工姓名
mysql> select name from worker where wages > 1000 and wages < 2000;
Empty set (0.00 sec)
i:列出所有陈姓和李姓的职工姓名
mysql> select name from worker where name like '陈%' or name like '李%';
+--------+
| name   |
+--------+
| 李四   |
+--------+
1 row in set (0.01 sec)
j:列出所有部门号为102和103的职工号、姓名、党员否
mysql> select worker_id,name from worker where politics!='党员' and department_id<=>102 or department_id<=>103;
+-----------+--------+
| worker_id | name   |
+-----------+--------+
|      1004 | 赵六   |
+-----------+--------+
1 row in set (0.00 sec)
k:将职工表worker中的职工按出生的先后顺序排序
mysql> select borth_date from worker order by borth_date;
+------------+
| borth_date |
+------------+
| 1983-06-08 |
| 1990-07-01 |
| 1992-12-30 |
| 1994-09-05 |
| 1996-09-02 |
| 1997-02-08 |
+------------+
6 rows in set (0.00 sec)
l:显示工资最高的前3名职工的职工号和姓名
mysql> select worker_id,name from worker order by wages desc  limit 0,3;
+-----------+--------+
| worker_id | name   |
+-----------+--------+
|      1003 | 王亮   |
|      1004 | 赵六   |
|      1005 | 钱七   |
+-----------+--------+
3 rows in set (0.00 sec)
m:求出各部门党员的人数
mysql> select department_id,count(name) from worker where politics='党员'group by department_id;
+---------------+-------------+
| department_id | count(name) |
+---------------+-------------+
|           102 |           3 |
+---------------+-------------+
1 row in set (0.00 sec)
n:统计各部门的工资和平均工资
mysql> select department_id,sum(wages),avg(wages) from worker group by department_id;
+---------------+------------+-------------+
| department_id | sum(wages) | avg(wages)  |
+---------------+------------+-------------+
|           101 |    6700.00 | 3350.000000 |
|           102 |   23300.00 | 5825.000000 |
+---------------+------------+-------------+
2 rows in set (0.00 sec)
o:列出总人数大于4的部门号和总人数
mysql> select department_id,count(department_id) from worker group by(department_id) having count(department_id) > 4;
Empty set (0.00 sec)

 

你可能感兴趣的:(数据库,mysql)