美文网首页
SQL语句多表查询时用到的链接

SQL语句多表查询时用到的链接

作者: 天夭夭 | 来源:发表于2018-01-29 18:10 被阅读0次

内链接:INNER JOIN

作用:根据两个或多个表中的列之间的关系,从这些表中查询数据。

注意:内连接是从结果中删除其他被连接表中没有匹配行的所有行,所以内连接可能会丢失信息。

重点:内连接,只查匹配行。

示例: select 投影 from table1 inner join table2 on table1.ID=table2.ID where 选择

外链接:full join

作用:查询结果显示左右表中的所有行,当某一个表中没有匹配的行时,则另一个表的选择列表列包含空值(NULL)如果有则显示全部数据。

重点:显示所有行。

示例:select 投影 from table1 full join join table2 on table1.ID=table2.ID where 选择

左链接: left join

作用:查询结果为 left join 子句中左表上有的行,而不仅仅链接列所有匹配的行,如果左表中的某行在右表中没有被匹配到的话,则在相关的结果行中右表的所有选择列均为控制NULL

重点:左连接,查询left join 子句中左表上有的行

示例:select 投影 from table1 left join table2 on table1.ID=table2.ID where 选择

右链接: right join

与左链接效果相反,不再赘述

交叉链接:Cross join

作用:交叉连接返回左表中的所有行,左表中的每一行与右表中的所有行组合。交叉连接也称作笛卡尔积。(笛卡尔积:(a+b)*(c+d)=ac+ad+bc+bd)

重点:简单查询两张表组合,这是求笛卡儿积,效率最低,查询速度极慢,需避免使用。

示例:select * from  table1 cross join table2 == select * from table1,table2

select * from table1  cross join table2 where table1. 条件列名= table2.条件列名 (带条件的)

应用实例:

select a.username,a.realname,c.frame_name,c.name

from [CLS_User] as a

left join [CLHR_Archives] as b on a.archivesid=b.id

left join [CLM_CompanyJob] as c on b.postdepartment=c.frameid and b.JName=c.name

where a.username='HQH'

相关文章

网友评论

      本文标题:SQL语句多表查询时用到的链接

      本文链接:https://www.haomeiwen.com/subject/kokfzxtx.html