1.创建表
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.02 sec)
mysql> desc t_worker;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| department_id | int | NO | | NULL | |
| worker_id | int | NO | PRI | NULL | |
| worker_date | date | NO | | NULL | |
| wages | float(8,2) | NO | | NULL | |
| politics | varchar(10) | NO | | 群众 | |
| name | varchar(20) | NO | | NULL | |
| borth_date | date | NO | | NULL | |
+---------------+-------------+------+-----+---------+-------+
7 rows in set (0.01 sec)
2.插入数据
insert into t_worker values (101,1001,'2015-5-4',7500.00,'群众','张春燕','1990-7-1');
insert into t_worker values (101,1002,'2019-2-6',5200.00,'团员','李名博','1997-2--8');
insert into t_worker values (102,1003,'2008-1-4',10500.00,'党员','王博涵','1983-6-8');
insert into t_worker values (102,1004,'2016-10-10',5500.00,'群众','赵小军','1994-9-5');
insert into t_worker values (102,1005,'2014-4-1',8800.00,'党员','钱有财','1992-12-30');
insert into t_worker values (103,1006,'2019-5-5',5500.00,'党员','孙菲菲','1996-9-2');
3. 完成查询
(1)、显示所有职工的基本信息。
mysql> select * from t_worker;
+---------------+-----------+-------------+----------+----------+--------+------------+
| department_id | worker_id | worker_date | wages | politics | name | borth_date |
+---------------+-----------+-------------+----------+----------+--------+------------+
| 101 | 1001 | 2015-05-04 | 7500.00 | 群众 | 张春燕 | 1990-07-01 |
| 101 | 1002 | 2019-02-06 | 5200.00 | 团员 | 李名博 | 1997-02-08 |
| 102 | 1003 | 2008-01-04 | 10500.00 | 党员 | 王博涵 | 1983-06-08 |
| 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵小军 | 1994-09-05 |
| 102 | 1005 | 2014-04-01 | 8800.00 | 党员 | 钱有财 | 1992-12-30 |
| 103 | 1006 | 2019-05-05 | 5500.00 | 党员 | 孙菲菲 | 1996-09-02 |
+---------------+-----------+-------------+----------+----------+--------+------------+
6 rows in set (0.01 sec)
mysql>
(2)、查询所有职工所属部门的部门号,不显示重复的部门号。
# 去重查询
select distinct 字段列表 from 表名; # 使重复的数据只显示一条
# 在对字段进行去重的时候,要保证distinct在所有字段的最前面
# 如果distinct关键字后面有多个字段时,则会对多个字段进行组合去重,只有多个字段组合起来的值是相等的才会被去重
mysql> SELECT DISTINCT department_id FROM t_worker;
+---------------+
| department_id |
+---------------+
| 101 |
| 102 |
| 103 |
+---------------+
3 rows in set (0.00 sec)
mysql>
(3)求出所有职工的人数。
mysql> SELECT COUNT(*) AS total_workers FROM t_worker;
+---------------+
| total_workers |
+---------------+
| 6 |
+---------------+
1 row in set (0.01 sec)
mysql>
(4)、列出最高工和最低工资。
mysql> SELECT MAX(wages) AS max_wage, MIN(wages) AS min_wage FROM t_worker;
+----------+----------+
| max_wage | min_wage |
+----------+----------+
| 10500.00 | 5200.00 |
+----------+----------+
1 row in set (0.00 sec)
mysql>
(5)、列出职工的平均工资和总工资。
mysql> SELECT AVG(wages) AS avg_wage, SUM(wages) AS total_wage FROM t_worker;
+-------------+------------+
| avg_wage | total_wage |
+-------------+------------+
| 7166.666667 | 43000.00 |
+-------------+------------+
1 row in set (0.00 sec)
mysql>
(6)、创建一个只有职工工号、姓名和参加工作的新表,名为工作日期表。
mysql> CREATE TABLE work_date_table AS SELECT worker_id, name, worker_date FROM t_worker;
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from work_date_table;
+-----------+--------+-------------+
| worker_id | name | worker_date |
+-----------+--------+-------------+
| 1001 | 张春燕 | 2015-05-04 |
| 1002 | 李名博 | 2019-02-06 |
| 1003 | 王博涵 | 2008-01-04 |
| 1004 | 赵小军 | 2016-10-10 |
| 1005 | 钱有财 | 2014-04-01 |
| 1006 | 孙菲菲 | 2019-05-05 |
+-----------+--------+-------------+
6 rows in set (0.00 sec)
mysql>
(7)、显示所有党员的年龄。
mysql> SELECT name, TIMESTAMPDIFF(YEAR, borth_date, CURDATE()) AS age FROM t_worker WHERE politics = '党员';
+--------+------+
| name | age |
+--------+------+
| 王博涵 | 42 |
| 钱有财 | 32 |
| 孙菲菲 | 28 |
+--------+------+
3 rows in set (0.00 sec)
或 表达式显示
(8)、列出工资在4000—8000之间的所有职工姓名。
mysql> SELECT name FROM t_worker WHERE wages BETWEEN 4000 AND 8000;
+--------+
| name |
+--------+
| 张春燕 |
| 李名博 |
| 赵小军 |
| 孙菲菲 |
+--------+
4 rows in set (0.00 sec)
(9)、列出所有孙姓和李姓的职工姓名。
mysql> SELECT name FROM t_worker WHERE name LIKE '孙%' OR name LIKE '李%';
+--------+
| name |
+--------+
| 李名博 |
| 孙菲菲 |
+--------+
2 rows in set (0.00 sec)
(10)、列出所有部门号为102和103且不是党员的职工号、姓名。
mysql> SELECT worker_id, name FROM t_worker WHERE department_id IN (102,103) AND politics != '党员';
+-----------+--------+
| worker_id | name |
+-----------+--------+
| 1004 | 赵小军 |
+-----------+--------+
1 row in set (0.00 sec)
(11)、将职工表t_worker中的职工按出生的先后顺序排序。
mysql> SELECT * FROM t_worker ORDER BY borth_date;
+---------------+-----------+-------------+----------+----------+--------+------------+
| department_id | worker_id | worker_date | wages | politics | name | borth_date |
+---------------+-----------+-------------+----------+----------+--------+------------+
| 102 | 1003 | 2008-01-04 | 10500.00 | 党员 | 王博涵 | 1983-06-08 |
| 101 | 1001 | 2015-05-04 | 7500.00 | 群众 | 张春燕 | 1990-07-01 |
| 102 | 1005 | 2014-04-01 | 8800.00 | 党员 | 钱有财 | 1992-12-30 |
| 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵小军 | 1994-09-05 |
| 103 | 1006 | 2019-05-05 | 5500.00 | 党员 | 孙菲菲 | 1996-09-02 |
| 101 | 1002 | 2019-02-06 | 5200.00 | 团员 | 李名博 | 1997-02-08 |
+---------------+-----------+-------------+----------+----------+--------+------------+
6 rows in set (0.00 sec)
(12)、显示工资最高的前3名职工的职工号和姓名。
mysql> SELECT worker_id, name FROM t_worker ORDER BY wages DESC LIMIT 3;
+-----------+--------+
| worker_id | name |
+-----------+--------+
| 1003 | 王博涵 |
| 1005 | 钱有财 |
| 1001 | 张春燕 |
+-----------+--------+
3 rows in set (0.00 sec)
(13)、求出各部门党员的人数。
> SELECT department_id, COUNT(*) AS party_members
-> FROM t_worker
-> WHERE politics = '党员'
-> GROUP BY department_id;
+---------------+---------------+
| department_id | party_members |
+---------------+---------------+
| 102 | 2 |
| 103 | 1 |
+---------------+---------------+
2 rows in set (0.00 sec)
(14)、统计各部门的工资和平均工资并保留2位小数
mysql> SELECT department_id,
-> ROUND(SUM(wages), 2) AS total_wages,
-> ROUND(AVG(wages), 2) AS avg_wages
-> FROM t_worker
-> GROUP BY department_id;
+---------------+-------------+-----------+
| department_id | total_wages | avg_wages |
+---------------+-------------+-----------+
| 101 | 12700 | 6350 |
| 102 | 24800 | 8266.67 |
| 103 | 5500 | 5500 |
+---------------+-------------+-----------+
3 rows in set (0.00 sec)
(15)、列出总人数大于等于3的部门号和总人数。
mysql> SELECT department_id, COUNT(*) AS total_workers
-> FROM t_worker
-> GROUP BY department_id
-> HAVING total_workers >= 3;
+---------------+---------------+
| department_id | total_workers |
+---------------+---------------+
| 102 | 3 |
+---------------+---------------+
1 row in set (0.00 sec)
mysql>