建表
mysql> create table work(
-> dept_id int(11) not null comment '部门号',
-> staff_id int(11) not null comment '职工号',
-> work_time date not null comment '工作时间',
-> wage float(8.2) not null comment '工资',
-> poli_face varchar(20) not null default '群众 'comment '政治面貌',
-> birth date not null comment '出生年月',
-> primary key(staff_id)
-> )engine=innodb default charset=utf8 row_format=dynamic;
插入数据
insert into work values(101,1001,'2015-5-4',3500.00,'群众','1990-7-1','张三');
insert into work values(101,1002,'2017-5-4',3200.00,'团员','1997-2-8','李四');
insert into work values(101,1003,'2011-1-4',8500.00,'党员','1983-6-8','王亮');
insert into work values(102,1004,'2016-10-10',5500.00,'群众','1994-9-5','赵六');
insert into work values(102,1005,'2014-4-1',4800.00,'党员','1992-12-30','钱七');
insert into work values(102,1006,'2017-5-5',4500.00,'0,'党员','1996-9-2','孙八');
1、查询所有员工信息
mysql> select *from work;
2、查询所有职工所属部门的部门号,不显示重复的部门号
mysql> select distinct dept_id from work;
3、求出所有职工的人数。
mysql> select count(name_name) from work;
4、列出最高工和最低工资。
mysql> select max(wage) as '最高工资',min(wage) as '最低
工资' from work;
5、列出职工的平均工资和总工资。
mysql> select avg(wage) as '平均工资',sum(wage) as '.工资' from work;
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
mysql> create table workdate select staff_id,name_name frmom work;
mysql> select *from workdate;
7.显示所有女职工的年龄。
由于之前建表的时候没有说明年龄,性别,所以现在得在表中加入年龄、性别列
mysql> update work
-> set age=19,gender='F'
-> where name_name='张三';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update work
-> set age=21,gender='F'
-> WHERE name_name='李四';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update work
-> set age=22,gender='F'
-> where name_name='王亮';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update work
-> set gender='M'
-> where name_name='赵六';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update work
-> set gender='M'
-> where name_name='钱七';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update work
-> set gender='M'
-> where name_name='孙八';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select *from work;
8、列出所有姓刘的职工的职工号、姓名和出生日期。
mysql> select staff_id,name_name,birth
-> from work
-> where name_name='刘%';
work表中没有刘姓人员,所以查询不到
9、列出1960年以前出生的职工的姓名、参加工作日期。
mysql> select name_name,work_time
-> from work
-> where birth < '1960-1-1';
Empty set (0.00 sec)
10、列出工资在1000一2000之间的所有职工姓名。
mysql> select name_name from work where wage between 1000 and 2000;
Empty set (0.01 sec)
11、列出所有陈姓和李姓的职工姓名。
mysql> select name_name
-> from work
-> where name_name like '陈%' or name_name like '李%';
+-----------+
| name_name |
+-----------+
| 李四 |
+-----------+
1 row in set (0.00 sec)
12、列出所有部门号为2和3的职工号、姓名、党员否
mysql> select staff_id,name_name,poli_face
-> from work
-> where dept_id in (102,103) and poli_face != '党 员';
+----------+-----------+-----------+
| staff_id | name_name | poli_face |
+----------+-----------+-----------+
| 1004 | 赵六 | 群众 |
+----------+-----------+-----------+
1 row in set (0.01 sec)
13、将职工表worker中的职工按出生的先后顺序排序。
mysql> select name_name,birth
-> from work
-> order by (birth);
+-----------+------------+
| name_name | birth |
+-----------+------------+
| 王亮 | 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)
14、显示工资最高的前3名职工的职工号和姓名。
mysql> select staff_id,name_name
-> from work
-> order by(wage) desc limit 0, 3;
+----------+-----------+
| staff_id | name_name |
+----------+-----------+
| 1003 | 王亮 |
| 1004 | 赵六 |
| 1005 | 钱七 |
+----------+-----------+
3 rows in set (0.00 sec)
15、求出各部门党员的人数。
mysql> select dept_id ,count(name_name) from work where poli_face='党员' group by(dept_id);
+---------+------------------+
| dept_id | count(name_name) |
+---------+------------------+
| 101 | 1 |
| 102 | 2 |
+---------+------------------+
16、统计各部门的工资和平均工资
mysql> select dept_id,sum(wage),avg(wage) from work grouup by(dept_id);
+---------+-----------+-------------------+
| dept_id | sum(wage) | avg(wage) |
+---------+-----------+-------------------+
| 101 | 15200 | 5066.666666666667 |
| 102 | 14800 | 4933.333333333333 |
+---------+-----------+-------------------+
2 rows in set (0.00 sec)
17、列出总人数大于2的部门号和总人数。
mysql> select dept_id,count(name_name) from work groupby(dept_id) having count(name_name) > 2;
+---------+------------------+
| dept_id | count(name_name) |
+---------+------------------+
| 101 | 3 |
| 102 | 3 |
+---------+------------------+
2 rows in set (0.00 sec)