资源预览内容
第1页 / 共2页
第2页 / 共2页
亲,该文档总共2页全部预览完了,如果喜欢就下载吧!
资源描述
轻松读懂左连接和右连接轻松读懂左连接和右连接一、本质:一、本质:将两个表的数据依据一定条件横向连接起来。建表语句: create table test1(id int, name varchar(10) )create table test2(id int, name varchar(10) )sql 数据: INSERT INTO test1 VALUES (1,2); INSERT INTO test1 VALUES (1,2); INSERT INTO test1 VALUES (2,1); INSERT INTO test1 VALUES (2,2); INSERT INTO test1 VALUES (3,1);INSERT INTO test2 VALUES (1,2); INSERT INTO test2 VALUES (2,1); INSERT INTO test2 VALUES (2,2);二、实例二、实例左连接 sql:select * from test1 a left join test2 b on a.id=b.id 如下为左连接示例图:1,理解左连接查询方式(以两表 id 相等作为 on 的条件): 先将左表数据查出,然后根据 on 后面的条件,将右表中凡是 id 与左表 id 相等的记录都 查出来,与匹配的左表记录依次排成一行或多行,若无匹配的记录,则显示 null。 举例:select * from test1 a left join test2 b on a.id=b.id 说明:先将左表数据查出,然后根据 on 后面的条件,将右表中凡是 id 与左表 id 相等的 记录都查出来,与匹配的左表记录依次排成一行或多行,若无匹配的记录,则显示 null。2,理解了左连接之后,右连接也就不难了。 右连接语句:select * from test1 a right join test2 b on a.id=b.id 结果如图:3,而内连接,则是把 ID 只存在于某一个表中的记录给过滤了。 以下 3 条 sql 语句等价: select * from test1 a join test2 b on a.id=b.id select * from test1 a inner join test2 b on a.id=b.id select * from test1 a , test2 b where a.id=b.id 内连接查询结果:本文源自博客
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号