建表
mysql> create table work(
-> ‘部门号’ 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(‘职工号’)
-> );
mysql> insert into work values(101, 1001, '2015-5-4', 3500, '群众', '张三', '1990-7-1');
mysql> insert into work values(101, 1002, '2017-2-6', 3200, '团员', '李四', '1997-2-8');
mysql> insert into work values(101, 1003, '2011-1-4', 8500, '党员', '王亮', '1983-6-8');
mysql> insert into work values(102, 1004, '2016-10-10', 5500, '群众', '赵六', '1994-9-5');
mysql> insert into work values(102, 1005, '2014-4-1', 4800, '党员', '钱七', '1992-12-30');
mysql> insert into work values(102, 1006, '2017-5-5', 4500, '党员', '孙八', '1996-9-2');
mysql> select *from work;
mysql> select distinct ‘部门号’ from work;
mysql> select count(*) from work;
mysql> select max(‘工资’), min(‘工资’) from work;
mysql> select avg(‘工资’)as '平均工资', sum(‘工资’)as '工资总和' from work;
mysql> create table work_date select ‘职工号’, ‘姓名’, ‘工作时间’ from work;
mysql> select ‘职工号’, ‘姓名’, ‘出生日期’ from work where ‘姓名’ like '张%';
mysql> select ‘姓名’, ‘工作时间’ from work where ‘出生日期’ < '1994-1-1';
mysql> select ‘工资’, ‘姓名’ from work where ‘工资’ between 3000 and 4000;
mysql> select ‘姓名’ from work where ‘姓名’ like '李%' or ‘姓名’ like '王%';
mysql> select ‘职工号’, ‘姓名’, ‘政治面貌’ from work where ‘部门号’ in (101, 102) and ‘政治面貌’='党员';
mysql> select *from work order by ‘出生日期’;
mysql> select ‘职工号’, ‘姓名’, ‘工资’ from work order by ‘工资’ desc limit 3;
mysql> select ‘部门号’, count(*) from work where ‘政治面貌’='党员' group by ‘部门号’ having count(*)>1;
mysql> select ‘部门号’, sum(‘工资’), avg(‘工资’) from work group by ‘部门号’ ;
mysql> select ‘部门号’, count(*) from work group by ‘部门号’ having count(8)>2;