比如我的select 一个表中有id1、id2,,此两个id的“id1-人名”及“id2-人名”都是从person表中获取人名
那么如何
select description,id1,id1-人名,id2,id2-人名 from worder
出来呢?
select description,id1,(select 人名 from worder er ,person son where er.id1=son.id)id1-人名,id2,(select 人名 from worder er ,person son where er.id2=son.id)id2-人名 from worder
有什么方法更加简单,因为实际上,查询的条件不止这些,有很多
------解决方案--------------------
如果你的建表语句如下,就必须引用两次 person 表
create table t
( description varchar(30) ,
id1 int ,
id2 int
)
另外,你的语句应该这样写
select description,
id1,
(select 人名 from person son where er.id1=son.id)id1-人名,
id2,
(select 人名 from person son where er.id2=son.id)id2-人名
from worder er
------解决方案--------------------
尝试是否可以用视图屏蔽查询条件?
------解决方案--------------------
提供一种其他的写法,列转行,再行转列,B表访问次数少了
但是至于那个执行时间快,不确定,具体执行下或看一下执行计划吧
TABLEA code1、code2、code3
TABLEB code、codename
SELECT MAX(DECODE(A.CONAME,'code1',B.codeName)),
MAX(DECODE(A.CONAME,'code2',B.codeName)),
MAX(DECODE(A.CONAME,'code3',B.codeName))
FROM
(select code1 as code,'code1' as colname,rowid as id from TABLEA
union all
select code2 as code,'code2' as colname,rowid as id from TABLEA
union all
select code3 as code,'code3' as colname,rowid as id from TABLEA) A,TABLEB B
WHERE A.CODE=B.CODE
GROUP BY A.ID
------解决方案--------------------
子查询换成连接会不会清晰一点?
select description,id1,a.人名 id1人名,id2,b.人名 id2人名
from worder er left join person a on er.id1=a.id
left join person b on er.id2=b.id