SQL学习(一):多表连查

SQL学习(一):多表连查

 

1.笛卡尔积:两个集合的相乘 4*9=36

关键字:无

使用范围:容易产生重复数据

select * from emp,dept;

2.交叉连接

关键字:cross join

使用范围:容易产生重复数据

select * from emp cross join dept;

2.1交叉连接 -内连接-显式内连接

关键字:inner join ...on...

select * from emp inner join dept on emp.dept_id = dept.id;

2.2交叉连接 -内连接-隐式内连接

关键字:where

select * from emp,dept where emp.dept_id = dept.id;

2.3交叉连接 -内连接-自连接

关键字:取别名

select a.*,b.name from emp a,emp b where a.leader = b.id;

2.4交叉连接 -外连接-左外连接

关键字:left join .... on .....

使用范围:

左外连接:左表为主表,右表为从表 主表所有数据都显示,从表匹配数据才显示

select a.*,b.name from emp a left join emp b on a.leader = b.id;

2.5交叉连接 -外连接-右外连接

关键字:right join .... on .....

使用范围:

右外连接:右表为主表,左表为从表

主表所有数据都显示,从表匹配数据才显示

右外连接:当sql比较复杂时,不易调换表的位置,视情况使用右外连接

select * from emp right join dept on emp.dept_id = dept.id;

3.on 和 where 的区别:on 两张表如何关联,where 两张表关联之后的筛选

4.查询不匹配的数据:is null

select * from emp left join dept on emp.dept_id = dept.id where dept.id is null;

select * from emp right join dept on emp.dept_id = dept.id where emp.dept_id is null;

5.实战

5.1子查询:嵌套查询&where 条件过滤

关键字:select A from B where C group by D order by E;

实例:

查询大于平均工龄的员工信息

select avg(year) from emp;
select * from emp where year > 3;

select * from emp where year >
(select avg(year) from emp);

5.2 any :任意一个 & all:所有 & in:固定值 & not in & exists:是否存在结果

关键字:any all

select * from emp where year > any
(select year from emp);

select * from emp where year >= all
(select year from emp);

关键字:in 等价于 固定值 等价于 =any

select * from emp where year < 1;

select * from dept where id = any
(select dept_id from emp where year < 1);

select * from dept where id in
(select dept_id from emp where year < 1);

关键字:not in 等价于 != all

select * from dept where id not in
(select dept_id from emp where year < 1);

select * from dept where id != all
(select dept_id from emp where year < 1);

关键字:EXISTS 是否存在结果

select * from dept where id in
(select dept_id from emp where year < 1);

select * from dept where EXISTS
(select dept_id from emp where year > 100);

select * from dept where EXISTS
(select dept_id from emp
where year < 1 and emp.dept_id = dept.id);

select * from dept where not EXISTS
(select dept_id from emp
where year < 1 and emp.dept_id = dept.id);

 


建表语句

DROP TABLE IF EXISTS dept; CREATE TABLE dept ( id int(11) NOT NULL COMMENT '部门id', name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '部门名称', PRIMARY KEY (id) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;


-- Records of dept


INSERT INTO dept VALUES (1, '人力资源部'); INSERT INTO dept VALUES (2, '网络服务部'); INSERT INTO dept VALUES (3, '法务部'); INSERT INTO dept VALUES (4, '生产技术部');

DROP TABLE IF EXISTS emp; CREATE TABLE emp ( id int(11) NOT NULL COMMENT '员工编号', name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '员工姓名', dept_id int(11) NULL DEFAULT NULL COMMENT '部门编号', leader int(11) NULL DEFAULT NULL COMMENT '直属领导id', year int(11) NULL DEFAULT NULL COMMENT '在职年限', is_enable int(11) NULL DEFAULT NULL COMMENT '是否在职 1在职 0离职', PRIMARY KEY (id) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;


-- Records of emp


INSERT INTO emp VALUES (1, '李雷', 1, 0, 4, 1); INSERT INTO emp VALUES (2, '韩梅梅', 1, 1, 3, 1); INSERT INTO emp VALUES (3, '小明', 1, 1, 0, 1); INSERT INTO emp VALUES (4, '小红', 1, 3, 10, 1); INSERT INTO emp VALUES (5, '老王', 2, 0, 7, 1); INSERT INTO emp VALUES (6, '小丸子', 2, 0, 1, 1); INSERT INTO emp VALUES (7, '大头儿子', 0, 8, 0, 1); INSERT INTO emp VALUES (8, '小头爸爸', 3, 0, 1, 1); INSERT INTO emp VALUES (9, '容嬷嬷', 0, 8, 1, 1);

 

你可能感兴趣的:(SQL学习(一):多表连查)