根据记录值有条件的查询(可能有点复杂)

大道如海 2004-10-13 02:03:43
不用存储过程,只能用SQL文

TA表
ID
kind
no

MA表
no
name

MB表
no
name

能否根据TA的kind值来动态选择MA或者MB表来进行查询

如kind=10
则TA.no = MA.no

kind=20
则TA.no = MB.no



...全文
134 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
大道如海 2004-10-19
  • 打赏
  • 举报
回复
这样可以吗
SELECT TA.ID,
TA.kind,
TA.no,
DECODE(TA.kind,10,MA.NAME,20,MB.NAME,'')
FORM TA,
MA,
MB
WHERE TA.no = MA.no(+)
AND TA.no = MB.no(+)
suxiao 2004-10-15
  • 打赏
  • 举报
回复
用decode试试!
liuyi8903 2004-10-15
  • 打赏
  • 举报
回复
用sql应该解决不了.
biliky 2004-10-13
  • 打赏
  • 举报
回复
select decode(t.id,10,a.no,20,b.no,null) from ta t,
(select no from MA where t.no=MA.no) a,
(select no from MB where t.no=MB.no) b;

楼主还想追求到何种完美方才罢休?
大道如海 2004-10-13
  • 打赏
  • 举报
回复
大道如海 2004-10-13
  • 打赏
  • 举报
回复
楼上的好像和用UNION的效果差不错
bzszp 2004-10-13
  • 打赏
  • 举报
回复
嵌套查询
select ta.*,decode(id,10,select name from ma where ta.no=ma.no,
20,select name from mb where ta.no=mab.no,null) name from ta;
大道如海 2004-10-13
  • 打赏
  • 举报
回复
原先我也用union解决
但是如果原来有100行SQL文
UNION一下就200行
如果有10种情况,则就是1000行,
感觉不是很好,
不知道能不能在WHERE里解决
大道如海 2004-10-13
  • 打赏
  • 举报
回复
case...when
可以用在WHERE子句里吗
freddy2003 2004-10-13
  • 打赏
  • 举报
回复
试一下,没测试过
select case kind=10 when then (select ta.id from ta,ma where ta.kind=10 and ta.no=ma.no)
else (select ta.id from ta,mb where ta.kind=20 and ta.no=mb.no) end from tA;
freddy2003 2004-10-13
  • 打赏
  • 举报
回复
Simple CASE Example
For each customer in the sample oe.customers table, the following statement lists the credit limit as "Low" if it equals $100, "High" if it equals $5000, and "Medium" if it equals anything else.

SELECT cust_last_name,
CASE credit_limit WHEN 100 THEN 'Low'
WHEN 5000 THEN 'High'
ELSE 'Medium' END
FROM customers;

CUST_LAST_NAME CASECR
-------------------- ------
...
Bogart Medium
Nolte Medium
Loren Medium
Gueney Medium

Searched CASE Example
The following statement finds the average salary of the employees in the sample table oe.employees, using $2000 as the lowest salary possible:

SELECT AVG(CASE WHEN e.salary > 2000 THEN e.salary
ELSE 2000 END) "Average Salary" from employees e;

Average Salary
--------------
6461.68224

xhyph 2004-10-13
  • 打赏
  • 举报
回复
好像不太可能吧,,,,
光表名就没办法选择,,表结构有问题
freddy2003 2004-10-13
  • 打赏
  • 举报
回复
用case...when
bzszp 2004-10-13
  • 打赏
  • 举报
回复
select * from ta,ma where ta.kind=10 and ta.no=ma.no
union all
select * from ta,mb where ta.kind=20 and ta.no=mb.no;

17,086

社区成员

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

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