admin管理员组文章数量:1032538
SQL各种联接的说明
/* create table table1(id int,name varchar(10)) create table table2(id int,score int) create table table3(id int,score int)
insert into table1 select 1,'yang' insert into table1 select 1,'zhi' insert into table1 select 2,'chen' insert into table1 select 4,'jack'
insert into table2 select 1,90 insert into table2 select 2,100 insert into table2 select 1,70 insert into table2 select 3,70 insert into table2 select 3,1
insert into table3 select 1,90 insert into table3 select 5,90 insert into table3 select 10,90 insert into table3 select 12,90 insert into table3 select 11,90 insert into table3 select 15,90 insert into table3 select 110,90 insert into table3 select 112,90 */ select * from table1 select * from table2 select * from table3
/* 左连接:left join 或 left outer join 左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。 若右表有多条记录与左表匹配,则匹配得到记录数为左表匹配行*右表相应匹配行数 如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。 sql语句 */ select * from table1 left outer join table2 on table1.id=table2.score --right outer join table3 on table3.id=table2.id /* 右向外联接是左向外联接的反向联接。将返回右表的所有行。 如果右表的某行在左表中没有匹配行,则将为左表返回空值 若左表有多条记录与右表匹配,则匹配得到记录数为表右匹配行*左表相应匹配行数 */ select * from table1 right join table2 on table1.id=table2.id /* 完整外部联接:full join 或 full outer join 完整外部联接返回左表和右表中的所有行。 当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。 如果表之间有匹配行,则整个结果集行包含基表的数据值。 注释:返回左右连接的和(见上左、右连接)
*/ select * from table1 full join table2 on table1.id=table2.id
/* 内连接:join 或 inner join 内联接是用比较运算符比较要联接列的值的联接 注释:只返回符合条件的table1和table2的列 等价(与下列执行效果相同) A:select a.*,b.* from table1 a,table2 b where a.id=b.id B:select * from table1 cross join table2 where table1.id=table2.id (注:cross join后加条件只能用where,不能用on)
*/
select * from table1 join table2 on table1.id=table2.id
/* 交叉连接(完全) 在没有 WHERE 子句的交叉联接将产生联接所涉及的表的笛卡尔积。 第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。(table1和table2交叉连接产生4*4=16条记录)
交叉连接:cross join (不带条件where...) 等价(与下列执行效果相同) A:select * from table1,table2
*/ select * from table1 cross join table2
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2014-09-18,如有侵权请联系 cloudcommunity@tencent 删除joinselect连接数据insertSQL各种联接的说明
/* create table table1(id int,name varchar(10)) create table table2(id int,score int) create table table3(id int,score int)
insert into table1 select 1,'yang' insert into table1 select 1,'zhi' insert into table1 select 2,'chen' insert into table1 select 4,'jack'
insert into table2 select 1,90 insert into table2 select 2,100 insert into table2 select 1,70 insert into table2 select 3,70 insert into table2 select 3,1
insert into table3 select 1,90 insert into table3 select 5,90 insert into table3 select 10,90 insert into table3 select 12,90 insert into table3 select 11,90 insert into table3 select 15,90 insert into table3 select 110,90 insert into table3 select 112,90 */ select * from table1 select * from table2 select * from table3
/* 左连接:left join 或 left outer join 左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。 若右表有多条记录与左表匹配,则匹配得到记录数为左表匹配行*右表相应匹配行数 如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。 sql语句 */ select * from table1 left outer join table2 on table1.id=table2.score --right outer join table3 on table3.id=table2.id /* 右向外联接是左向外联接的反向联接。将返回右表的所有行。 如果右表的某行在左表中没有匹配行,则将为左表返回空值 若左表有多条记录与右表匹配,则匹配得到记录数为表右匹配行*左表相应匹配行数 */ select * from table1 right join table2 on table1.id=table2.id /* 完整外部联接:full join 或 full outer join 完整外部联接返回左表和右表中的所有行。 当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。 如果表之间有匹配行,则整个结果集行包含基表的数据值。 注释:返回左右连接的和(见上左、右连接)
*/ select * from table1 full join table2 on table1.id=table2.id
/* 内连接:join 或 inner join 内联接是用比较运算符比较要联接列的值的联接 注释:只返回符合条件的table1和table2的列 等价(与下列执行效果相同) A:select a.*,b.* from table1 a,table2 b where a.id=b.id B:select * from table1 cross join table2 where table1.id=table2.id (注:cross join后加条件只能用where,不能用on)
*/
select * from table1 join table2 on table1.id=table2.id
/* 交叉连接(完全) 在没有 WHERE 子句的交叉联接将产生联接所涉及的表的笛卡尔积。 第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。(table1和table2交叉连接产生4*4=16条记录)
交叉连接:cross join (不带条件where...) 等价(与下列执行效果相同) A:select * from table1,table2
*/ select * from table1 cross join table2
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2014-09-18,如有侵权请联系 cloudcommunity@tencent 删除joinselect连接数据insert本文标签: SQL各种联接的说明
版权声明:本文标题:SQL各种联接的说明 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1747952115a2232698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论