在SELECT中,选择中的相关子查询将rowID转换为rowname

时间:2019-05-12 14:53:00

标签: sql subquery oracle-sqldeveloper correlated-subquery

如何组合这两组代码? 表格:geregistreerd g,乐器i和插入id

我要显示g.voornaammuzikantg.achternaammuzikanti.naaminstrumentid.familie

g通过i与表instrumentid链接 表i与表ID和indelingid链接

-强化1:

SELECT TRIM(' ' FROM g.voornaammuzikant), TRIM(' ' FROM g.achternaammuzikant),
    (SELECT i.naaminstrument 
    from instrument i
    where i.instrumentid = g.instrumentid) as "naam instrument"
from geregistreerd g
order by g.voornaammuzikant;

-强化2:

SELECT i.naaminstrument, 
(SELECT id.familie
from indeling id 
where i.indelingid = id.indelingid) as "familie",

(SELECT id.onderfamilie
from indeling id 
where i.indelingid = id.indelingid) as "onderfamilie"


from instrument i
order by i.naaminstrument;

组合

SELECT TRIM(' ' FROM g.voornaammuzikant), 
TRIM(' ' FROM g.achternaammuzikant),

(SELECT i.naaminstrument
        (SELECT id.familie
        from indeling id 
        where i.indelingid = id.indelingid) as "familie"
from instrument i
where i.instrumentid = g.instrumentid) as "naam instrument"  
from geregistreerd g
order by g.voornaammuzikant;

1 个答案:

答案 0 :(得分:0)

如果可以简化事情,为什么要以复杂的方式做事情?简单的联接有什么问题?像这样:

select g.voornaammuzikant, 
       g.achternaammuzikant, 
       i.naaminstrument, 
       id.familie
from geregistreerd g join instrument i on i.instrumentid = g.instrumentid
join indeling id on id.indelingid = i.indelingid
order by g.voornaammuzikant;

顺便说一句,您发布的TRIM看起来不太好。 SELECT TRIM(' ' FROM g.voornaammuzikant)当然是错误的;我不知道你想说什么。也许从这些值中删除多余的空间?如果存在,那么-该列的数据类型是否为CHAR?如果是这样,请考虑切换到VARCHAR2,因为前者将值填充到该列的整个长度为止。