SQL表连接图解
<p>可以通过图看下</p>
<p>
<img title="SQL表连接图解" alt="SQL表连接图解" src="https://zhuji.jb51.net/uploads/img/202305/eebaceee9fcfff49fd91a31494a50e6e.jpg"></p>
<p>
多表查询分为 内、外连接</p>
<p>
外连接分为左连接(left join 或left outer join)、右连接(right join 或者 right outer join)、和完整外部连接 (full join 或者 full outer join)</p>
<p>
<strong>左连接(left join 或 left outer join)的结果就是left join子句中的左表的所有行,而不仅仅是链接列所匹配的行,如果左表中的某行在右表中没有匹配,则在相关联的结果行中右表的所有选择列均为空值(null)</strong></p>
<p>
sql语法 select * from table1 left join table2 on table1.条件列名 = table2.条件列名;</p>
<p>
注释: 显示的就是table1中的所有列和能匹配的列</p>
<p>
<strong>右连接(right join 或 right outer join )在这里不做多说这左连接很象但是是相反的,只说一下语法</strong></p>
<p>
select *from table1 right join table2 on table1. 条件列= table2.条件列</p>
<p>
<strong>完全外部连接(full join 或 full outer join)</strong></p>
<p>
显示左右表中的所有行,当某一个表中没有匹配的行时,则另一个表的选择列表列包含空值(null)如果有则显示全部数据</p>
<p>
sql语法:</p>
<p>
select *from table1 full join table2 on table1.条件列名= table2.条件列名</p>
<p>
<strong>内连接:</strong><br>
概念:内连接就是用比较运算符比较要用连接列的值的连接</p>
<p>
内连接(join 或者inner join )</p>
<p>
sql语法:</p>
<p>
select *fron table1 join table2 on table1.条件列名 = table2.条件列名</p>
<p>
返回符合匹配条件的两表列</p>
<p>
等价于:</p>
<p>
select a* ,b* from table1 a ,table2 b where a.条件列名 =b.条件列名<br>
select *form table1 cross join table2 where table1.条件列名 = table2.条件列名(注: cross join 后面不能跟on 只能用where)</p>
<p>
交叉连接(完全)</p>
<p>
概念:没有用where子句的交叉连接将产生连接所涉及的笛卡尔积第一个表的行数乘以第二个表的行数等于笛卡尔积和结果集的大小</p>
<p>
交叉连接: cross join(不带条件where,如果带返回或显示的是匹配的行数)</p>
<p>
sql语法:</p>
<p>
select *from table1 cross join table2</p>
<p>
如果有条件(where)</p>
<p>
select * from table1 cross join table2 where table1. 条件列名= table2.条件列名</p>
<p>
等价于</p>
<p>
select *from table1,table2 (不带where)</p>
頁:
[1]