既然是主从表,那么认为a表中有记录的不一定在b表中存在,而b表中存在
的id一定在a表存在,不然叫什么主从表。
假设id为主键
select a.id,max(a.col1)...,max(b.col1),max(b.col2) from a,b
where a.id = b.id(+) group by a.id;
既然随便取一条,那么就取最大的一条。
注意:如果主键有几个,需要在group by后面加。
由于在a表中用的是主键分组,那么max(a.col1)=a.col1不影响的。
而主从表表示a表一条记录对应b表多条记录,所以max会将其他
记录去掉。
重做如下:
取a与b相等的记录中b的最大rowid。这样保证只取出一条记录。而且这条记录的id在a表里存在。
然后用b表的这一条记录与a表作外连接:
select * from a,(select * from b where rowid=(select max(b.rowid) from a,b where a.id=b.id )) x where a.id= x.id(+)
/
得出结果 3 万条记录。
ID NAME
------ --------------------
1 H
2 H
3 H
1 G
2 G
3 G
1 F
比较笨的方法!(主_从表不太了解,哈哈)
select * from song_main a,(select id,name from (select id,name,rank() over(partition by id order by name )m from song_det)
where m=1) b
where a.id=b.id(+)
/
其中song_main是主表,song_det是从表,楼主试试!
每个表都只有id,name2个字段
或许有参考价值!!