mysql> create table worker(
-> dept_id int(11) not null,
-> emp_id int (11) not null,
-> work_time date not null,
-> salary float(8,2) not null,
-> poli_face varchar(10) not null default '群众',
-> name varchar(20) not null,
-> birth date not null,
-> primary key(emp_id)
-> )engine=innodb default charset=utf8 row_format=dynamic;
向里面插入数据
mysql> insert into worker values(101,1001,'2015-5-4',3500.00,'群众','张三','1990-7-1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into worker values(101,1002,'2017-2-6',3200.00,'团员','李四','1997-2-8');
Query OK, 1 row affected (0.00 sec)
mysql> insert into worker values(102,1003,'2011-2-4',8500.00,'党员','王亮','1983-6-8');
Query OK, 1 row affected (0.02 sec)
mysql> insert into worker values(102,1004,'2016-10-10',5500.00,'群众','赵六','1994-9-5');
Query OK, 1 row affected (0.00 sec)
mysql> insert into worker values(102,1005,'2014-4-1',4800.00,'党员','钱七','1992-12-30');
Query OK, 1 row affected (0.00 sec)
mysql> insert into worker values(102,1006,'2017-5-5',4500.00,'党员','孙八','1996-9-2');
Query OK, 1 row affected (0.01 sec)
select * from worker;
select distinct dept_id from worker;
select count(name) from worker;
select max(salary) as '最高工资',min(salary) as '最低工资' from worker;
select avg(salary) as '平均工资',sum(salary) as '总工资' from worker;
mysql> create table workdate select emp_id,name from worker;
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from workdate;
+--------+--------+
| emp_id | name |
+--------+--------+
| 1001 | 张三 |
| 1002 | 李四 |
| 1003 | 王亮 |
| 1004 | 赵六 |
| 1005 | 钱七 |
| 1006 | 孙八 |
+--------+--------+
6 rows in set (0.00 sec)
首先worker表当中暂时没有年龄和性别属性所以要增加
mysql> alter table worker add age tinyint default 18;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table worker add gender char(1) check(gender in ('F','M'));
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
然后向里面给每个人插入年龄和姓别
mysql> update worker set age=19,gender='F' where name='张三';
mysql> update worker set age=20,gender='F' where name='李四';
mysql> update worker set age=21,gender='M' where name='王亮';
mysql> update worker set gender='M' where name='赵六';
mysql> update worker set gender='M' where name='钱七';
mysql> update worker set gender='M' where name='孙八';
此时我们可以查看一下表的内容
最后查询即可
select age from worker where gender='F';
select emp_id,name,birth from worker where name='刘%';
因为我们表中并没有姓刘的所以应该是查询不到的
select name,work_time from worker where birth < '1960-1-1';
select name from worker where salary between 1000 and 2000;
select name from worker where name like '李%' or name like '陈%';
select emp_id,name from worker where poli_face != '党员' and dept_id in (102,103);
select name,birth from worker order by(birth);
select emp_id,name from worker order by(salary) desc limit 0,3;
select dept_id ,count(name) from worker where poli_face='党员' group by(dept_id);
因为此时统计出来的全是102部门的,为了验证是否写的正确我又插入了一个部门为101的数据
insert into worker values(101,1007,'2017-1-1',4000.00,'党员','陈启','1990-2-2',18,'M');
然后查询
可以看到是正确的
16、统计各部门的工资和平均工资
select dept_id,sum(salary),avg(salary) from worker group by(dept_id);
select dept_id,count(name) from worker group by(dept_id) having count(name) > 4;
因为我们的表中并没有部门人数大于4的所以查不到,我们可以查一下部门人数大于二的验证以下正确性
select dept_id,count(name) from worker group by(dept_id) having count(name) > 2;