如何用SQL语句解决这样的问题?急!

adhere 2003-08-26 10:22:04
有三张表TABLE1、TABLE2、TABLE3,
其中TABLE1、TABLE2的字段一样,都是由四个字段:
A,B,C,D组成;
TABLE3由两个字段组成:A,B。
现在要实现的功能是:
输入一个值INPUT
当TABLE1中A、B分别等于TABLE3中的A、B时 and INPUT = TABLE1.C,取得TABLE1中的值,
否则当TABLE2中A、B分别等于TABLE3中的A、B时 and INPUT = TABLE2.C,取得TABLE2中的值。
请问:能不能用SQL语句实现(程序先不去判断TABLE1是否有没有对应的值)

...全文
39 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jpabcd 2003-08-28
  • 打赏
  • 举报
回复
trigger行吗?
onejune4450 2003-08-27
  • 打赏
  • 举报
回复
select a.* from table1 a ,table3 b where a.a=b.a and a.b=b.b and a.c= INPUT
union
select c.* from table2 c ,table3 d where c.a=d.a and c.b=d.b and c.c= INPUT
and not exists ( select 1 from table1 e where c.a=e.a and c.b=e.b )
beckhambobo 2003-08-27
  • 打赏
  • 举报
回复
写成这样也属失败了,如此复杂,麻烦楼主以后记录不断增大,是否可以忍受
adhere 2003-08-27
  • 打赏
  • 举报
回复
不好意思,漏了一些
select
decode(table1.A,null,table2.A,table1.A) as A,
decode(table1.A,null,table2.B,table1.B) as B,
decode(table1.A,null,table2.C,table1.C) as C,
decode(table1.A,null,table2.D,table1.D) as D
from table1,table2,table3,
( select table3.A as A,
table3.B as B,
decode(table1.A,NULL,'table2','table1') as flag
from table1,table3
where table3.A = table1.A(+)
and table3.B = table1.B(+)
) tempTable
where table1.A(+) = table3.A
and table1.B(+) = table3.B
and table2.A(+) = table3.A
and table2.B(+) = table3.B
and tempTable.A(+) = table3.A
and tempTable.B(+) = table3.B
and (
(tempTable.flag = 'table1' and table1.C = INPUT)
or
(tempTable.flag = 'table2' and table2.C = INPUT)
)
adhere 2003-08-27
  • 打赏
  • 举报
回复
问题已解决:
select
decode(table1.A,null,table2.A,table1.A) as A,
decode(table1.A,null,table2.B,table1.B) as B,
decode(table1.A,null,table2.C,table1.C) as C,
decode(table1.A,null,table2.D,table1.D) as D
from table1,table2,table3,
( select A,
B,
decode(table1.A,NULL,'table2','table1') as flag
from table1,table3
where table3.A = table1.A(+)
and table3.B = table1.B(+)
) tempTable
where table1.A(+) = table3.A
and table1.B(+) = table3.B
and table2.A(+) = table3.A
and table2.B(+) = table3.B
and tempTable.A(+) = table3.A
and tempTable.B(+) = table3.B
and (
(tempTable.flag = 'table1' and table1.C = INPUT)
or
(tempTable.flag = 'table2' and table2.C = INPUT)
)
其中A、B为主KEY,NOT NULL。
谢谢大家
DongBCB 2003-08-26
  • 打赏
  • 举报
回复
建议用存储过程或函数实现就行了,不用这样浪费时间
adhere 2003-08-26
  • 打赏
  • 举报
回复
TO zjhclf(寂寞撩人) :一个sql语句应该不能实现if..else的逻辑,那多个sql语句能实现吗?或者说,sql语句里能嵌套if..else的逻辑吗?请具体说明,或提供相应的文章地址。
adhere 2003-08-26
  • 打赏
  • 举报
回复
TO LGQDUCKY(飘):第一个嵌套的select中的table2应该是table3吧,但是这样会出现冗余的数据,比如table1和table2都有的数据。而且,table2中的数据可能比TABLE1的数据要多,如果table1中没数据,这是没问题的,但如果table1中有数据,就不行了。
LGQDUCKY 2003-08-26
  • 打赏
  • 举报
回复
select * from (select a.* from table1 a ,table2 b where a.a=b.a and a.b=b.b and
a.c= INPUT) a ,
(select a.* from table2 a ,table3 b where a.c= INPUT and a.a=b.a and a.b=b.b ) b
where a.a=b.a and a.b=b.b;
lbl20020123 2003-08-26
  • 打赏
  • 举报
回复
解决复杂的问题最好化整为零,想用单个SQL实现比较困难,可以用PL/SQL实现。



zjhclf 2003-08-26
  • 打赏
  • 举报
回复
应该用过程实现,一个sql语句应该不能实现if..else的逻辑,继续关注
onejune4450 2003-08-26
  • 打赏
  • 举报
回复
select a.* from table1 a ,table3 b where a.a=b.a and a.b=b.b and a.c= INPUT
union
select a.* from table2 a ,table3 b where a.a=b.a and a.b=b.b and a.c= INPUT

17,078

社区成员

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

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