select出的两个集合的操作,一个是另外一个的子集合

夕阳那边 2015-10-18 06:11:42
在网上找了很久很久的,可能有一天了吧,还是没有找到解决方案。。。
希望各位看到了能够高抬贵手,帮帮我,虽然没有实物相赠,我从内心里面先感激各位了。。。

废话不多说了。。
问题是这样的

我有两个表,比如第一个表为:
NAME
---------------表1
A
A
A
B
B
C
第二个表的长相为
NMAE
---------------表2
A
B
表2其实是表1 的一个子集
我希望能够从数据库中,将两个表中能够查询到 类似于补集但是不是很像的结果,结果是
NAME
------------------
A
A
B
C
也就是表1中有的,表2中也有的,这一行就在结果集中去掉,不过只去掉一次



再次先感谢各位大神了
...全文
150 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyon2008 2015-10-19
  • 打赏
  • 举报
回复
select name from tab1 where col_pk not in (select min(col_pk) r from tab1 inner join tab2 on tab1.name = tab2.nmae group by tab1.name) 说明:col_pk为tab1的主键字段,相信应该有
美到心痛 2015-10-18
  • 打赏
  • 举报
回复
加个row_number就行了,例如: select ta.a,row_number() over(partition by a order by 1) rn from (select 'A' a from dual union all select 'A' from dual union all select 'A' from dual union all select 'B' from dual union all select 'B' from dual union all select 'C' from dual ) ta minus select tb.a,row_number() over(partition by a order by 1) from (select 'A' a from dual union all select 'B' from dual ) tb ; 或者 select ta.* from (select a,row_number() over(partition by a order by 1) rn from (select 'A' a from dual union all select 'A' from dual union all select 'A' from dual union all select 'B' from dual union all select 'B' from dual union all select 'C' from dual ) ) ta left join (select a,row_number() over(partition by a order by 1) rn from (select 'A' a from dual union all select 'B' from dual ) ) tb on ta.a=tb.a and ta.rn=tb.rn where tb.a is null;
夕阳那边 2015-10-18
  • 打赏
  • 举报
回复

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧