数据库的高级查询
- 1. 提要
- 2. 数据统计
-
- 2.1 聚合函数
- 2.2 分组查询_group by
- 2.3 having子句_筛选时想使用聚合函数时
- 3. 多表连接查询
-
- 3.1 内连接_inner join
- 3.2 外连接(左外连接)_left join
- 3.3 右外连接_right join
- 3.4 练习
- 3.5 小结
- 4. 子查询
-
- 4.1 from子查询
- 4.2 单行子查询与多行子查询
- 5. SQL测试环境
1. 提要
2. 数据统计
2.1 聚合函数
2.2 分组查询_group by
2.3 having子句_筛选时想使用聚合函数时
3. 多表连接查询
3.1 内连接_inner join
**练习: **
3.2 外连接(左外连接)_left join
3.3 右外连接_right join
3.4 练习
3.5 小结
4. 子查询
4.1 from子查询
4.2 单行子查询与多行子查询
in,all,any,exists使用
5. SQL测试环境
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `t_dept`;
CREATE TABLE `t_dept` (
`deptno` int(0) NOT NULL COMMENT '部门编号',
`dname` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '部门名称',
`loc` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '部门标志',
PRIMARY KEY (`deptno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '部门表' ROW_FORMAT = Dynamic;
INSERT INTO `t_dept` VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO `t_dept` VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO `t_dept` VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO `t_dept` VALUES (40, 'OPERATIONS', 'BOSTON');
DROP TABLE IF EXISTS `t_emp`;
CREATE TABLE `t_emp` (
`empno` int(0) NOT NULL COMMENT '员工ID',
`ename` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '姓名',
`job` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL COMMENT '职位',
`mgr` int(0) NULL DEFAULT NULL COMMENT '领导ID',
`hiredate` date NULL DEFAULT NULL COMMENT '入职日期',
`sal` decimal(10, 2) NULL DEFAULT NULL COMMENT '基本工资',
`comm` decimal(10, 2) NULL DEFAULT NULL COMMENT '绩效',
`deptno` int(0) NULL DEFAULT NULL COMMENT '部门编号',
PRIMARY KEY (`empno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '员工表' ROW_FORMAT = Dynamic;
INSERT INTO `t_emp` VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800.00, NULL, 20);
INSERT INTO `t_emp` VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600.00, 300.00, 30);
INSERT INTO `t_emp` VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250.00, 500.00, 30);
INSERT INTO `t_emp` VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975.00, NULL, 20);
INSERT INTO `t_emp` VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250.00, 1400.00, 30);
INSERT INTO `t_emp` VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850.00, NULL, 30);
INSERT INTO `t_emp` VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450.00, NULL, 10);
INSERT INTO `t_emp` VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000.00, NULL, 20);
INSERT INTO `t_emp` VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000.00, NULL, 10);
INSERT INTO `t_emp` VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500.00, 0.00, 30);
INSERT INTO `t_emp` VALUES (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100.00, NULL, 20);
INSERT INTO `t_emp` VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950.00, NULL, 30);
INSERT INTO `t_emp` VALUES (7902, '田曦薇', 'ANALYST', 7566, '1981-12-03', 3000.00, NULL, 20);
INSERT INTO `t_emp` VALUES (7934, '与海', 'CLERK', 7782, '1982-01-23', 1300.00, NULL, NULL);
DROP TABLE IF EXISTS `t_salgrade`;
CREATE TABLE `t_salgrade` (
`grade` int(0) NOT NULL COMMENT '等级',
`losal` decimal(10, 2) NULL DEFAULT NULL COMMENT '下限',
`hisal` decimal(10, 2) NULL DEFAULT NULL COMMENT '上限',
PRIMARY KEY (`grade`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci COMMENT = '工资等级表' ROW_FORMAT = Dynamic;
INSERT INTO `t_salgrade` VALUES (1, 700.00, 1200.00);
INSERT INTO `t_salgrade` VALUES (2, 1201.00, 1400.00);
INSERT INTO `t_salgrade` VALUES (3, 1401.00, 2000.00);
INSERT INTO `t_salgrade` VALUES (4, 2001.00, 3000.00);
INSERT INTO `t_salgrade` VALUES (5, 3001.00, 9999.00);
SET FOREIGN_KEY_CHECKS = 1;