============求一条SQL语句(Oracle数据库)============

Yizhoujimoqitian 2012-06-17 12:08:29
背景:
1:Oracle数据库

2:有一个数据表tableA,它包括一列TelphoneNum。

3:还有若干个表,tableB,tableC,tableD,.....tableN,这些表中也包括了一个TelphoneNum的字段。

要求实现:

1:tableA中的TelphoneNum,如果没有在tableB,tableC,tableD,.....tableN出现过,那么从tableA表中把此TelphoneNum相关的信息选取出来。

2:如果tableA中的TelphoneNum在tableB,tableC,tableD,.....tableN中的任意一个表中出现过。那么也要选取出来,标记他是在那一个表中出现过。

3:没有出现的TelphoneNum要排在已经出现过的TelphoneNum之前

最后效果,例如:
---------------------------------------------------------
手机品牌|厂商|手机号码(TelphoneNum)|是否出现
---------------------------------------------------------
A |A |13912345678 |
B |B |13912345679 |

C |C |13001234567 |在tableB中出现过
D |D |13000000000 |在tableN中出现过
---------------------------------------------------------


谢谢。
...全文
169 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cool326236882 2012-07-17
  • 打赏
  • 举报
回复
decode
smnzg 2012-07-17
  • 打赏
  • 举报
回复

select tableA.手机品牌, tableA.厂商,
tableA.TelphoneNum || (select decode(nvl(count(1),0), 0, '', '|在tableB中出现过') from tableB where tableA.TelphoneNum =tableB.TelphoneNum)
|| (select decode(nvl(count(1),0), 0, '', '|在tableC中出现过') from tableC where tableA.TelphoneNum =tableC.TelphoneNum)
|| .........
|| (select decode(nvl(count(1),0), 0, '', '|在tableN中出现过') from tableN where tableA.TelphoneNum =tableN.TelphoneNum)
from tableA


比较长,不知道是不是符合你的要求.
smnzg 2012-07-17
  • 打赏
  • 举报
回复

select tableA.手机品牌, tableA.厂商,
tableA.TelphoneNum || (select decode(nvl(count(1),0), 0, '', '|在tableB中出现过') from tableB where tableA.TelphoneNum =tableB.TelphoneNum)
|| (select decode(nvl(count(1),0), 0, '', '|在tableC中出现过') from tableC where tableA.TelphoneNum =tableC.TelphoneNum)
|| .........
|| (select decode(nvl(count(1),0), 0, '', '|在tableN中出现过') from tableN where tableA.TelphoneNum =tableN.TelphoneNum)
from tableA


比较长,不知道是不是符合你的要求.
阿冷 2012-06-18
  • 打赏
  • 举报
回复
select t1.TelphoneNum,t2.tablename
from tableA t1,
(select TelphoneNum,'tableB' tablename from tableB
union all
select TelphoneNum,'tableC' tablename from tableC
union all
...
select TelphoneNum,'tableN' tablename from tableN) t2
where t1.TelphoneNum = t2.TelphoneNum(+)
勿勿 2012-06-18
  • 打赏
  • 举报
回复
union all ..
007-x 2012-06-17
  • 打赏
  • 举报
回复
先把tableB到tableN建一个view,包含TelphoneNum和表名。
create view tableBtoN as
select TelphoneNum,'tableB' tablename from tableB
union all
select TelphoneNum,'tableC' tablename from tableC
union all
...
select TelphoneNum,'tableN' tablename from tableN

然后
select TelphoneNum,(select tablename from tableBtoN t2 where t1.TelphoneNum=t2.TelphoneNum and rownum=1) "是否出现过" from tableA

coolkisses 2012-06-17
  • 打赏
  • 举报
回复
直接将这些表关联,也是一种办法。

是否出现过 一列,换成 A B C ... N 的 N列

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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