今天一个同事问到一个SQL语句的写法,整理到这里。
为完成这个需求用到了外连接和内连接,对sql的连接语句的理解比较有帮助。
SQL> create table b (id number,file varchar2(10));
SQL> create table b (id number,f varchar2(10));
SQL> insert into a values (1,1,2);
SQL> insert into a values (2,3,'');
SQL> insert into a values (3,4,3);
SQL> commit;
SQL> insert into b values (1,'A:\>');
SQL> insert into b values (2,'B:\>');
SQL> insert into b values (3,'C:\>');
SQL> insert into b values (4,'D:\>');
SQL> commit;
SQL> select * from a;
C1 C2 C3
---------- ---------- ----------
1 1 2
2 3
3 4 3
SQL> select * from b;
ID F
---------- ----------
1 a
2 b
3 c
4 d
通过上面的过程构建出下面的两张表:
a表:
c1 c2 c3
1 1 2
2 3
3 4 3
b表:
id f
1 A:\>
2 B:\>
3 C:\>
4 D:\>
表含义介绍:
a表:c1列表示序号,c2列表示文件号,c3列表示与c2列相关联的文件号
b表:id列表示文件号,f列表示文件所在的磁盘位置
需求:取得每个文件所在的位置和对应文件位置的列表,如果对应文件位置不存在需要显示为空。
处理思路:
1.通过外连接使b表中id值与a表中c2值相等构造出temp1表
SQL> select b.id,b.f,a.c3 from a,b where a.c2(+)=b.id;
2.temp1与自身进行自连接得到关联文件对应的磁盘位置
SQL> select temp1.id,temp1.f,b.f from temp1, b where temp1.c3=b.id(+) order by 1;
最终,一条SQL语句搞定需求:
SQL> select temp1.id,temp1.f,b.f from (select b.id,b.f,a.c3 from a,b where a.c2(+)=b.id) temp1, b where temp1.c3=b.id(+) order by 1;
ID F F
---------- ---------- ----------
1 A:\> B:\>
2 B:\>
3 C:\>
4 D:\> C:\>
-- The End --
为完成这个需求用到了外连接和内连接,对sql的连接语句的理解比较有帮助。
SQL> create table b (id number,file varchar2(10));
SQL> create table b (id number,f varchar2(10));
SQL> insert into a values (1,1,2);
SQL> insert into a values (2,3,'');
SQL> insert into a values (3,4,3);
SQL> commit;
SQL> insert into b values (1,'A:\>');
SQL> insert into b values (2,'B:\>');
SQL> insert into b values (3,'C:\>');
SQL> insert into b values (4,'D:\>');
SQL> commit;
SQL> select * from a;
C1 C2 C3
---------- ---------- ----------
1 1 2
2 3
3 4 3
SQL> select * from b;
ID F
---------- ----------
1 a
2 b
3 c
4 d
通过上面的过程构建出下面的两张表:
a表:
c1 c2 c3
1 1 2
2 3
3 4 3
b表:
id f
1 A:\>
2 B:\>
3 C:\>
4 D:\>
表含义介绍:
a表:c1列表示序号,c2列表示文件号,c3列表示与c2列相关联的文件号
b表:id列表示文件号,f列表示文件所在的磁盘位置
需求:取得每个文件所在的位置和对应文件位置的列表,如果对应文件位置不存在需要显示为空。
处理思路:
1.通过外连接使b表中id值与a表中c2值相等构造出temp1表
SQL> select b.id,b.f,a.c3 from a,b where a.c2(+)=b.id;
2.temp1与自身进行自连接得到关联文件对应的磁盘位置
SQL> select temp1.id,temp1.f,b.f from temp1, b where temp1.c3=b.id(+) order by 1;
最终,一条SQL语句搞定需求:
SQL> select temp1.id,temp1.f,b.f from (select b.id,b.f,a.c3 from a,b where a.c2(+)=b.id) temp1, b where temp1.c3=b.id(+) order by 1;
ID F F
---------- ---------- ----------
1 A:\> B:\>
2 B:\>
3 C:\>
4 D:\> C:\>
-- The End --
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/519536/viewspace-557229/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/519536/viewspace-557229/