一、单表查询 素材: 表名:worker-- 表中字段均为中文,比如 部门号 工资 职工号 参加工作 等 CREATE TABLE `worker` ( `部门号` 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 (`职工号`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生 日期`) VALUES (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1'); INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生 日期`) VALUES (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8'); INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生 日期`) VALUES (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8'); INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生 日期`) VALUES (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5'); INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生 日期`) VALUES (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30'); INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生 日期`) VALUES (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');
select * from worker; +-----------+-----------+--------------+---------+--------------+--------+--------------+ | 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 | +-----------+-----------+--------------+---------+--------------+--------+--------------+ | 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 | | 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 | | 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 | | 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 | | 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 | | 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 | +-----------+-----------+--------------+---------+--------------+--------+--------------+
SELECT DISTINCT `部门号` FROM worker; +-----------+ | 部门号 | +-----------+ | 101 | | 102 | +-----------+
mysql> select count(*) as '总人数' from worker; +-----------+ | 总人数 | +-----------+ | 6 | +-----------+
SELECT MAX(工资) AS '最高工资' FROM worker; +--------------+ | 最高工资 | +--------------+ | 8500.00 | +--------------+
select max(工资) as "最低工资" from worker; +--------------+ | 最低工资 | +--------------+ | 3200.00 | +--------------+
mysql> select avg(工资) as "平均工资" from worker; +--------------+ | 平均工资 | +--------------+ | 5000.000000 | +--------------+ mysql> select sum(工资) as "总工资" from worker; +-----------+ | 总工资 | +-----------+ | 30000.00 | +-----------+
create table `工作日期表`( `职工号` int(11) not null, `姓名` varchar(255) not null, `参加工作` varchar (255),primary key (`. 工号`));
alter table worker add `性别` varchar(255) not null; --加表性别的字段; alter table worker add `年龄` int(11) not null; --加表年龄的字段; update worker set `年龄` = 18, `性别` = "女" where `姓名` = "张三"; update worker set `年龄` = 25, `性别` = "男" where `姓名` = "李四"; update worker set `年龄` = 27, `性别` = "男" where `姓名` = "王亮"; update worker set `年龄` = 27, `性别` = "女" where `姓名` = "赵六"; update worker set `年龄` = 29, `性别` = "女" where `姓名` = "钱七"; update worker set `年龄` = 21, `性别` = "男" where `姓名` = "孙八"; select `年龄`,`姓名`,`性别` from worker where `性别`="女"; +--------+--------+--------+ | 年龄 | 姓名 | 性别 | +--------+--------+--------+ | 18 | 张三 | 女 | | 27 | 赵六 | 女 | | 29 | 钱七 | 女 | +--------+--------+--------+
select `职工号`,`姓名`,`出生日期` from worker where `姓名` like "刘%"; Empty set (0.00 sec)
mysql> select `姓名`,`工作时间` from worker where `出生日期`<1960; Empty set, 1 warning (0.00 sec)
mysql> select `姓名` from worker where 2000>`工资`>1000; Empty set (0.01 sec)
mysql> select `姓名` from worker where `姓名` like "陈%" or "李%"; Empty set, 1 warning (0.00 sec)
select `职工号`,`姓名`,`政治面貌` from worker where `部门 号`=2 and `部门号` = 3; Empty set (0.00 sec)
mysql> select * from worker order by `出生日期`; +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+ | 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 | 性别 | 年龄 | +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+ | 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 | 男 | 27 | | 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 | 女 | 18 | | 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 | 女 | 29 | | 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 | 女 | 27 | | 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 | 男 | 21 | | 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 | 男 | 25 | +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+
SELECT * from worker order by `工资` asc LIMIT 3; +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+ | 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 | 性别 | 年龄 | +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+ | 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 | 男 | 25 | | 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 | 女 | 18 | | 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 | 男 | 21 | +-----------+-----------+--------------+---------+--------------+--------+--------------+--------+--------+
select count(*) as "党员总人数" from worker where `政治面貌``="党员"; +-----------------+ | 党员总人数 | +-----------------+ | 3 | +-----------------+
select avg(工资) from worker where `部门号`= 101; +-------------+ | avg(工资) | +-------------+ | 3350.000000 | +-------------+ 1 row in set (0.00 sec)
mysql> select avg(工资) from worker where `部门号`= 102; +-------------+ | avg(工资) | +-------------+ | 5825.000000 | +-------------+ 1 row in set (0.00 sec)
mysql> select sum(工资) from worker where `部门号`= 102; +-------------+ | sum(工资) | +-------------+ | 23300.00 | +-------------+ 1 row in set (0.01 sec)
mysql> select sum(工资) from worker where `部门号`= 101; +-------------+ | sum(工资) | +-------------+ | 6700.00 | +-------------+ 1 row in set (0.00 sec)
SELECT count(*), `部门号` from worker GROUP BY `部门号` HAVING COUNT(*) > 1; +----------+-----------+ | count(*) | 部门号 | +----------+-----------+ | 2 | 101 | | 4 | 102 | +----------+-----------+
create table t_heji like t_hero;--创建 新表t_heji,结构与t_hero相同;
mysql> insert into t_heji select * from t_hero;---t_hero Query OK, 108 rows affected (0.04 sec) Records: 108 Duplicates: 0 Warnings: 0
mysql> insert into t_heji select * from t_lol; Query OK, 6 rows affected (0.01 sec) Records: 6 Duplicates: 0 Warnings: 0
mysql> insert into t_heji select * from t_lol1; Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into t_heji select * from t_lol2; Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0
mysql> alter table t_heji add `部门` varchar(255) not null;---引入部门字段 Query OK, 0 rows affected (0.35 sec) Records: 0 Duplicates: 0 Warnings: 0
在作业2的基础上将部门分离出来,形成两张表,完成多表关联查询
