1. 分类:
* 合并结果集(了解)
* 连接查询
* 子查询
合并结果集
* 要求被合并的表中,列的类型和列数相同
* union,去除重复行
* union all,不去除重复行
select * from cd/*cd表*/union allselect * from ab;/*ab表*
连接查询
1. 分类
* 内连接
* 外连接
> 左外连接
> 右外连接
> 全外连接(mysql不支持)
* 自然连接(属于一种简化方式)
2. 内连接
* 方言:select * from 表1 别名1, 表2 别名2 where 别名1.xx=别名2.xx
* 标准:select * from 表1 别名1 inner join 表2 别名2 on 别名1.xx=别名2.xx
* 自然:select * from 表1 别名1 natural join 表2 别名2
* 内连接查询出的所有记录都满足条件。
3. 外连接
* 左外:select * from 表1 别名1 left outer join 表2 别名2 on 别名1.xx=别名2.xx
> 左表记录无论是否满足条件都会查询出来,而右表只有满足条件才能出来。左表中不满足条件的记录,右表部分都为null
* 左外自然:select * from 表1 别名1 natural left outer join 表2 别名2 on 别名1.xx=别名2.xx
* 右外:select * from 表1 别名1 right outer join 表2 别名2 on 别名1.xx=别名2.xx
> 右表记录无论是否满足条件都会查询出来,而左表只有满足条件才能出来。右表不满足条件的记录,其左表部分都为null
* 右外自然:select * from 表1 别名1 natural right outer join 表2 别名2 on 别名1.xx=别名2.xx
* 全链接:可以使用union来完成全链接
子查询
:查询中有查询(查看select关键字的个数!)
1. 出现的位置:
* where后作为条件存在
* from后作为表存在(多行多列)
2. 条件
* (***)单行单列:select * from 表1 别名1 where 列1 [=、>、f539a70d3ea090bac4faa80192f58ccc=、<=、!=] (select 列 from 表2 别名2 where 条件)
* (**)多行单列:select * from 表1 别名1 where 列1 [in, all, any] (select 列 from 表2 别名2 where 条件)
* (*)单行多列:select * from 表1 别名1 where (列1,列2) in (select 列1, 列2 from 表2 别名2 where 条件)
* (***)多行多列:select * from 表1 别名1 , (select ....) 别名2 where 条件
以上就是关于mysql中多表查询详解的详细内容。
