--测试数据
create table tba(aid int,aname varchar(20))
insert tba select 1 ,'计算机系'
union all select 2 ,'经管系'
union all select 3 ,'物理系'
union all select 4 ,'艺术系'
go
create table tbb(bid int ,aid int,bname varchar(20))
insert tbb select 1, 1 ,'计算机系下专业1'
union all select 2, 1 ,'计算机系下专业2'
union all select 3, 2 ,'经管系下专业1'
union all select 4, 2 ,'经管系下专业2'
union all select 5, 4 ,'艺术系下专业1'
go
--查询
--删除测试
--drop table tba
--drop table tbb
create table tba(aid int,aname varchar(20))
insert tba select 1 ,'计算机系'
union all select 2 ,'经管系'
union all select 3 ,'物理系'
union all select 4 ,'艺术系'
go
create table tbb(bid int ,aid int,bname varchar(20))
insert tbb select 1, 1 ,'计算机系下专业1'
union all select 2, 1 ,'计算机系下专业2'
union all select 3, 2 ,'经管系下专业1'
union all select 4, 2 ,'经管系下专业2'
union all select 5, 4 ,'艺术系下专业1'
go
--查询
--删除测试
--drop table tba
--drop table tbb
int 改为 number,select后在加表名即可create table tba(aid number,aname varchar(20)) insert tba select 1 ,'计算机系' from dual
union all select 2 ,'经管系' from dual
union all select 3 ,'物理系' from dual
union all select 4 ,'艺术系' from dual
--希望查询结果如下:
aid aname bid bname
1 计算机系
1 计算机系下专业1
2 计算机系下专业2
2 经管系
3 经管系下专业1
4 经管系下专业2
3 物理系
4 艺术系
5 艺术系下专业1
--测试数据
create table tba(aid number,aname varchar2(20)) ;
insert into tba select 1 ,'计算机系' from dual
union all select 2 ,'经管系' from dual
union all select 3 ,'物理系' from dual
union all select 4 ,'艺术系' from dual;
commit;
create table tbb(bid int ,aid int,bname varchar2(20)) ;
insert into tbb select 1, 1 ,'计算机系下专业1' from dual
union all select 2, 1 ,'计算机系下专业2' from dual
union all select 3, 2 ,'经管系下专业1' from dual
union all select 4, 2 ,'经管系下专业2' from dual
union all select 5, 4 ,'艺术系下专业1' from dual ;
commit;
--查询
select tba.aid, tba.aname, tbb.bid, tbb.bname
from tba, tbb
where tba.aid = tbb.aid(+)
union
select tba.aid, tba.aname, null, null
from tba
order by 1, 2, 3 nulls first AID ANAME BID BNAME
---------- -------------------- ---------- --------------------
1 计算机系
1 计算机系 1 计算机系下专业1
1 计算机系 2 计算机系下专业2
2 经管系
2 经管系 3 经管系下专业1
2 经管系 4 经管系下专业2
3 物理系
4 艺术系
4 艺术系 5 艺术系下专业19 rows selected--删除测试
drop table tba
drop table tbb
--查询 修改一下
select decode(x.bid,null,x.aid) aid,
decode(x.bid,null,x.aname) aname,
x.bid,
x.bname
from(
select tba.aid, tba.aname, tbb.bid, tbb.bname
from tba, tbb
where tba.aid = tbb.aid(+)
union
select tba.aid, tba.aname, null, null
from tba
order by 1, 2, 3 nulls first)xAID ANAME BID BNAME
1 计算机系
1 计算机系下专业1
2 计算机系下专业2
2 经管系
3 经管系下专业1
4 经管系下专业2
3 物理系
4 艺术系
5 艺术系下专业1