数据库_连接查询
Posted jacquelineqa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库_连接查询相关的知识,希望对你有一定的参考价值。
连接查询
1,交叉连接
select * from emp;查询单个表
select * from emp,dept;查询两个表
select * from emp cross join dept;
笛卡尔积
2,内连接
显示内连接,标准内连接
select * from emp as a inner join dept as b on a.dept_id = b.id;
隐式内连接,where 在结果集的基础上进行条件筛选
select * from emp as a ,dept as b where a.dept_id=b.id;
内连接 join
select * from emp as a join dept as b on a.dept_id = b.id;
内连接 cross join
select * from emp as a cross join dept as b on a.dept_id = b.id;
mysql 里cross join on 和inner join on 结果一样
标准sql,cross join不能使用on,mysql支持on
特殊内连接,不等值内连接
select * from emp as a inner join dept as b on a.dept_id > b.id;
特殊内连接 自连接
select * from emp as a inner join emp as b on a.id =b.leader;
3,外连接
外连接显示的内容>=内连接
左外连接,左表为主表
select * from emp as a left outer join dept as b on a.dept_id = b.id;
右外连接,右表为主表
select * from emp as a right outer join dept as b on a.dept_id = b.id;
外连接,查询从表为空的数据
select * from emp as a left join dept as b on a.dept_id = b.id
where b.id is null;
on 和where的区别
on 两张表如何关联,where结果集做筛选
select * from emp as a left join dept as b on (a.dept_id = b.id and b.id is null);
on 优先where
select * from emp as a left join dept as b on a.dept_id = b.id where b.id is null;
4,全连接full ,unionq去重,union all不去重
select * from emp as a left join dept as b on a.dept_id = b.id
union
select * from emp as a right join dept as b on a.dept_id = b.id;
以上是关于数据库_连接查询的主要内容,如果未能解决你的问题,请参考以下文章
知识库-数据库_MySQL之高级数据查询:去重复组合查询连接查询虚拟表
第七周 Java语法总结之数据库大全_DDL_DML_DQL_约束_备份与还原_表的关系_三大范式_多表查询(内连接_外连接_子查询)_musql事务_隔离级别