两张表关联查询问题

puckgod 2011-12-12 11:28:41
有表A(id,col1,col2,col3),B(id,col1,col2,col3,col4),其中前4个字段是相同的,两个表通过id字段关联,现在需要关联查询符合条件的相关数据。
条件: 如果B表中的id字段值在A表中存在,那么取B中数据,否则取A中数据,结果只要前4个字段,需要用一条sql实现。
...全文
129 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zujinsheng 2011-12-12
  • 打赏
  • 举报
回复
靠..理解反意思了..


select B.id,B.col1,B.col2,B.col3 from B
union
select A.* from A,B
where A.id not in (select B.id from B)
zujinsheng 2011-12-12
  • 打赏
  • 举报
回复
select A.* from A
union
select B.id,B.col1,B.col2,B.col3 from A,B
where B.id not in (select A.id from A)
yixilan 2011-12-12
  • 打赏
  • 举报
回复
你执行试试。
select b.id, b.col1, b.col2, b.col3 from a, b
where a.id = b.id
union
select a.id, a.col1, a.col2, a.col3 from a
where not EXISTS (select 1 from a, b where a.id = b.id)
puckgod 2011-12-12
  • 打赏
  • 举报
回复
木有人?
y112102 2011-12-12
  • 打赏
  • 举报
回复
不好意思 少打了关键字
select a.id, rownum
from (select a.id
from a
inner join b on (a.id = b.id)
union
select a.id
from a
where not EXISTS (select 1 from a, b where a.id = b.id))
where rownum between 1 and 4
group by a.id
y112102 2011-12-12
  • 打赏
  • 举报
回复
select a.id,
rownum (select a.id
from a
inner join b on (a.id = b.id)
union
select a.id
from a
where not EXISTS
(select 1 from a, b where a.id = b.id))
where rownum between 1 and 4
group by a.id

17,140

社区成员

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

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