创建一个数据库
mysql> create database mydb1_zhigong;
Query OK, 1 row affected (0.02 sec)
使用数据库
use mydb1_zhigong;
Database changed
创建表
mysql> 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=utf8mb4 row_format=dynamic;
Query OK, 0 rows affected, 3 warnings (0.02 sec)
向表中插入数据
mysql> insert into `worker` (`部门号`, `职工号`,`工作时间`,`工资`,`政治面貌`,`姓名`,`出生日期`) values (101, 1001 , '2015-5-4',3500.00,'群众','张三','1990-7-1');
Query OK, 1 row affected (0.01 sec)
mysql> insert into `worker` (`部门号`, `职工号`,`工作时间`,`工资`,`政治面貌`,`姓名`,`出生日期`) values (102, 1002 , '201
7-2-6',3200.00,'团员','李四','1997-2-8');
Query OK, 1 row affected (0.01 sec)
mysql> insert into `worker` (`部门号`, `职工号`,`工作时间`,`工资`,`政治面貌`,`姓名`,`出生日期`) values (103, 1003 , '2011-1-4',8500.00,'党员','王亮','1983-6-8');
Query OK, 1 row affected (0.01 sec)
mysql> insert into `worker` (`部门号`, `职工号`,`工作时间`,`工资`,`政治面貌`,`姓名`,`出生日期`) values (104, 1004 , '201
6-10-10',5500.00,'群众','赵六','1994-9-5');
Query OK, 1 row affected (0.01 sec)
mysql> insert into `worker` (`部门号`, `职工号`,`工作时间`,`工资`,`政治面貌`,`姓名`,`出生日期`) values (102, 1005 , '2014-4-1',4800.00,'党员','钱七','1992-12-30');
Query OK, 1 row affected (0.01 sec)
实现要求
mysql> select * from worker;
+--------+--------+------------+---------+----------+------+------------+
| 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 |
+--------+--------+------------+---------+----------+------+------------+
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 102 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 |
| 103 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 |
| 104 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 |
| 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 |
+--------+--------+------------+---------+----------+------+------------+
5 rows in set (0.00 sec)
mysql> select distinct 部门号 from worker;
+--------+
| 部门号 |
+--------+
| 101 |
| 102 |
| 103 |
| 104 |
+--------+
4 rows in set (0.01 sec)
mysql> select count(职工号) as 职工总数 from worker;
+----------+
| 职工总数 |
+----------+
| 5 |
+----------+
1 row in set (0.01 sec)
mysql> select max(工资) as 最高工资, min(工资) as 最低工资 from worker;
+----------+----------+
| 最高工资 | 最低工资 |
+----------+----------+
| 8500.00 | 3200.00 |
+----------+----------+
1 row in set (0.00 sec)
mysql> select
-> avg(工资) as 平均工资,
-> sum(工资) as 总工资
-> from worker;
+-------------+----------+
| 平均工资 | 总工资 |
+-------------+----------+
| 5100.000000 | 25500.00 |
+-------------+----------+
1 row in set (0.00 sec)
mysql> create table 工作日期表 as
-> select 职工号,姓名, 工作时间 from worker;
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select 职工号,姓名,出生日期
-> from worker
-> where 姓名 like '张%';
+--------+------+------------+
| 职工号 | 姓名 | 出生日期 |
+--------+------+------------+
| 1001 | 张三 | 1990-07-01 |
+--------+------+------------+
1 row in set (0.00 sec)
mysql> select 姓名,工作时间
-> from worker
-> where 出生日期 <'1960-01-01';
Empty set (0.00 sec)
mysql> select 姓名
-> from worker
-> where 工资 between 100 and 2000;
Empty set (0.00 sec)
mysql> select 姓名
-> from worker
-> where 姓名 like '陈%' or 姓名 like '李%';
+------+
| 姓名 |
+------+
| 李四 |
+------+
1 row in set (0.00 sec)
mysql> select 职工号,姓名,政治面貌 as 是否党员
-> from worker
-> where 部门号 in (2,3);
Empty set (0.00 sec)
mysql> select * from worker
-> order by 出生日期 asc;
+--------+--------+------------+---------+----------+------+------------+
| 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 |
+--------+--------+------------+---------+----------+------+------------+
| 103 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 |
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 |
| 104 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 |
| 102 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 |
+--------+--------+------------+---------+----------+------+------------+
5 rows in set (0.00 sec)
mysql> select 职工号,姓名
-> from worker
-> order by 工资 desc
-> limit 3;
+--------+------+
| 职工号 | 姓名 |
+--------+------+
| 1003 | 王亮 |
| 1004 | 赵六 |
| 1005 | 钱七 |
+--------+------+
3 rows in set (0.00 sec)
mysql> select
-> 部门号,
-> sum(工资) as 部门总工资,
-> avg(工资) as 部门平均工资
-> from worker
-> group by 部门号;
+--------+------------+--------------+
| 部门号 | 部门总工资 | 部门平均工资 |
+--------+------------+--------------+
| 101 | 3500.00 | 3500.000000 |
| 102 | 8000.00 | 4000.000000 |
| 103 | 8500.00 | 8500.000000 |
| 104 | 5500.00 | 5500.000000 |
+--------+------------+--------------+
4 rows in set (0.00 sec)