sql
#(1)
mysql> select * from t_worker;
+---------------+---------+-------------+----------+----------+--------+------------+
| department_id | work_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.00 sec)
#(2)
mysql> select distinct department_id from t_worker;
+---------------+
| department_id |
+---------------+
| 101 |
| 102 |
| 103 |
+---------------+
3 rows in set (0.00 sec)
#(3)
mysql> select count(1) from t_worker;
+----------+
| count(1) |
+----------+
| 6 |
+----------+
1 row in set (0.00 sec)
#(4)
mysql> select max(wages) '最高工资',min(wages) '最低工资' from t_worker;
+----------+----------+
| 最高工资 | 最低工资 |
+----------+----------+
| 10500.00 | 5200.00 |
+----------+----------+
1 row in set (0.00 sec)
#(5)
mysql> select avg(wages) '平均工资',sum(wages) '总工资' from t_worker;
+-------------+----------+
| 平均工资 | 总工资 |
+-------------+----------+
| 7166.666667 | 43000.00 |
+-------------+----------+
1 row in set (0.00 sec)
#(6)
mysql> create table work_date(worker_id int(11) not null comment '部门号',worker_name varchar(20) not null comment '姓名',worker_date date not null comment '参加工作');
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> desc work_date;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| worker_id | int | NO | | NULL | |
| worker_name | varchar(20) | NO | | NULL | |
| worker_date | date | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
#(7)
mysql> select work_id,borth_date,timestampdiff(year,borth_date,curdate()) '党员年龄' from t_worker;
+---------+------------+----------+
| work_id | borth_date | 党员年龄 |
+---------+------------+----------+
| 1001 | 1990-07-01 | 34 |
| 1002 | 1997-02-08 | 27 |
| 1003 | 1983-06-08 | 41 |
| 1004 | 1994-09-05 | 29 |
| 1005 | 1992-12-30 | 31 |
| 1006 | 1996-09-02 | 27 |
+---------+------------+----------+
6 rows in set (0.00 sec)
#(8)
mysql> select name,wages from t_worker where wages between 4000 and 8000;
+--------+---------+
| name | wages |
+--------+---------+
| 张春燕 | 7500.00 |
| 李名博 | 5200.00 |
| 赵小军 | 5500.00 |
| 孙菲菲 | 5500.00 |
+--------+---------+
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)
mysql> select work_id,name from t_worker where department_id in(102,103) and politics!='党员';
+---------+--------+
| work_id | name |
+---------+--------+
| 1004 | 赵小军 |
+---------+--------+
1 row in set (0.00 sec)
#(11)
mysql> select * from t_worker order by borth_date;
+---------------+---------+-------------+----------+----------+--------+------------+
| department_id | work_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)
mysql> select work_id,name from t_worker order by wages desc limit 3;
+---------+--------+
| work_id | name |
+---------+--------+
| 1003 | 王博涵 |
| 1005 | 钱有财 |
| 1001 | 张春燕 |
+---------+--------+
3 rows in set (0.00 sec)
#(13)
mysql> select count(1),department_id from t_worker where politics='党员' group by department_id;
+----------+---------------+
| count(1) | department_id |
+----------+---------------+
| 2 | 102 |
| 1 | 103 |
+----------+---------------+
2 rows in set (0.00 sec)
#(14)
mysql> select sum(wages) '部门总工资',round(avg(wages),2) '平均工资',department_id from t_worker group by department_id;
+------------+----------+---------------+
| 部门总工资 | 平均工资 | department_id |
+------------+----------+---------------+
| 12700.00 | 6350 | 101 |
| 24800.00 | 8266.67 | 102 |
| 5500.00 | 5500 | 103 |
+------------+----------+---------------+
3 rows in set (0.00 sec)
#(15)
mysql> select department_id,count(1) from t_worker group by department_id having count(1)>=3;
+---------------+----------+
| department_id | count(1) |
+---------------+----------+
| 102 | 3 |
+---------------+----------+
1 row in set (0.00 sec)
`