请教一个SQL的写法

nol 2006-09-30 01:50:47
表1,表2列完全一样,比如都是A,B两个列。
取出表1,2中A列字段个数不一样,以及A列字段个数一样但B不一样的A列,SQL应该怎么写

如:

表1:
A B
1 100
1 101
2 103
3 104

表2:
A B
1 100
2 103
3 109

表1中A=1的有两个,表2中是1个,取出A=1
表1中A=2的有1个,表2中也是1个,且B字段也相等,不取出
表1中A=3的有1个,表2中也是1个,但B字段不相等,取出A=3。

这个SQL应该怎么写,谢谢了。
...全文
147 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wiler 2006-09-30
  • 打赏
  • 举报
回复
with
tab_a as (
select A,count(*) A_num
from tab_1
group by A
),
tab_b as (
select A,count(*) A_num
from tab_2
group by A
)
select tab_A.A from tab_A,tab_B
where tab_A.A=tab_B.A and tab_A.A_num<>tab_B.A_num
union all
select A
from tab_1 ,tab_2
where tab_1.A=tab_2.A
and tab_1.B<>tab_2.B
and tab_1.A in (
select tab_a.A from tab_A,tab_B
where tab_A.A=tab_B.A and tab_A.A_num=tab_B.A_num
)
and tab_2.A in (
select tab_a.A from tab_A,tab_B
where tab_A.A=tab_B.A and tab_A.A_num=tab_B.A_num
)

nol 2006-09-30
  • 打赏
  • 举报
回复
with
tab_a as (
select A,count(*) A_num
from tab_1
group by A
),
tab_b as (
select A,count(*) A_num
from tab_2
group by A
)

是什么意思?

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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