在查询中使用多个表返回空值

时间:2014-09-30 09:25:14

标签: sql toad

我是SQL的新手,只知道非常基础知识。如果有人可以帮助解决以下问题,我将不胜感激。这将返回字段具有值的所有值但是对于LO.Filurecode需要返回此值,其中存在空值以及让我识别并填充缺失的值。任何人都可以帮助看到下面的酷儿。

alter session set current_schema=maximo;

select
l.location,
l.description,
l.siteid,
l.type,
l.status,
lh.parent,
lo.failurecode,
lo.locpriority,
L.PLUSGSAFETYCRIT,
L.PLUSGENVCRIT,
L.PLUSGCOMCRIT,
L.PLUSGEXREGISTERED,
l.ta,
l.taqamanufacturer,
l.modelnum,
l.PLUSGLOCREFERENCE,
l.PLUSGPHYSLOC 
from
locations l, lochierarchy lh, locoper lo
where
l.location = lh.location
and l.location = lo.location
and l.siteid = lo.siteid
and l.siteid = lh.siteid
and l.type ='OPERATING'
and lh.systemid ='PRIMARY'
and l.siteid ='COA'
order by location

1 个答案:

答案 0 :(得分:0)

以下是您的查询,其中包含一些重构:

SELECT l.location,
    l.description,
    l.siteid,
    l.type,
    l.status,
    lh.parent,
    lo.failurecode,
    lo.locpriority,
    l.PLUSGSAFETYCRIT,
    l.PLUSGENVCRIT,
    l.PLUSGCOMCRIT,
    l.PLUSGEXREGISTERED,
    l.ta,
    l.taqamanufacturer,
    l.modelnum,
    l.PLUSGLOCREFERENCE,
    l.PLUSGPHYSLOC 
FROM locations l
LEFT JOIN lochierarchy lh ON lh.location = l.location
                            AND lh.siteid = l.siteid
                            AND lh.systemid = 'PRIMARY'
LEFT JOIN locoper lo ON lo.location = l.location
                       AND lo.siteid = l.siteid
WHERE l.type ='OPERATING'
and l.siteid ='COA'
ORDER BY l.location

我使用了显式联合语法(INNER JOIN, LEFT JOIN)而不是您使用的旧学校语法。它更具可读性,更容易理解。

希望这会有所帮助。