带有两个子选择的FULL OUTER JOIN给出“ORA-00918:列模糊定义”

时间:2012-02-06 10:50:58

标签: sql oracle join subquery ora-00918

两个Subselects都可以单独运行。

但我希望加入他们,他们总是给出“ORA-00918:列模糊定义”。我不明白为什么?

两个子选项都使用其他名称。

SELECT *
  FROM    (SELECT aods.user_id,
                  aods.firstname,
                  aods.lastname,
                  Aods.DEPARTMENT,
                  cods.jc_name
             FROM    personas aods
                  LEFT JOIN
                     user_jc cods
                  ON aods.user_id = cods.user_id
            WHERE cods.jc_name LIKE '%ADM%') ods
       FULL OUTER JOIN
          (SELECT abl.user_id,
                  abl.firstname,
                  abl.lastname,
                  Abl.DEPARTMENT,
                  bbl.ug_name
             FROM    personas abl
                  LEFT JOIN
                     ru_ug bbl
                  ON abl.user_id = bbl.user_id
            WHERE Bbl.UG_NAME LIKE '%ADM%'
                  AND bbl.rss_name = 'TIR') bl
       ON ods.user_id = bl.user_id

2 个答案:

答案 0 :(得分:6)

因为你在顶部做了select *

user_id
firstname
lastname
DEPARTMENT

存在于两个子查询中,使用select *,它将获得两个具有相同名称的列。

如果要合并两个子查询(以便生成的一组列为user_id, firstname, lastname, DEPARTMENT, jc_name, ug_name),您应该执行natural join

SELECT *
  FROM (SELECT aods.user_id, aods.firstname, aods.lastname, aods.department,
               cods.jc_name
          FROM personas aods LEFT JOIN user_jc cods
               ON aods.user_id = cods.user_id
         WHERE cods.jc_name LIKE '%ADM%') ods
       NATURAL JOIN
       (SELECT abl.user_id, abl.firstname, abl.lastname, abl.department,
               bbl.ug_name
          FROM personas abl LEFT JOIN ru_ug bbl ON abl.user_id = bbl.user_id
         WHERE bbl.ug_name LIKE '%ADM%' AND bbl.rss_name = 'TIR') bl

相反,如果您想要所有不同的列,则应明确列出它们:

 SELECT ods.user_id, ods.firstname, ods.lastname, ods.department, ods.jc_name,
        bl.user_id, bl.firstname, bl.lastname, bl.department, bl.ug_name
   FROM (SELECT aods.user_id, aods.firstname, aods.lastname, aods.department,
                cods.jc_name
           FROM personas aods LEFT JOIN user_jc cods
                ON aods.user_id = cods.user_id
          WHERE cods.jc_name LIKE '%ADM%') ods
        FULL OUTER JOIN
        (SELECT abl.user_id, abl.firstname, abl.lastname, abl.department,
                bbl.ug_name
           FROM personas abl LEFT JOIN ru_ug bbl ON abl.user_id = bbl.user_id
          WHERE bbl.ug_name LIKE '%ADM%' AND bbl.rss_name = 'TIR') bl
        ON ods.user_id = bl.user_id

在评论中,您说要在每个列上使用外部联接nvl()。对我来说它工作正常:

SELECT NVL (ods.user_id, bl.user_id), NVL (ods.firstname, bl.firstname),
       NVL (ods.lastname, bl.lastname), NVL (ods.department, bl.department),
       ods.jc_name, bl.ug_name
  FROM (SELECT '1' user_id, 'ods2' firstname, NULL lastname,
               'ods3' department, 'ods4' jc_name
          FROM DUAL) ods
       FULL OUTER JOIN
       (SELECT '1' user_id, NULL firstname, 'bl2' lastname, 'bl3' department,
               'bl4' ug_name
          FROM DUAL) bl ON ods.user_id = bl.user_id
你正在制作一些额外的东西,比如order by吗?

答案 1 :(得分:0)

指定列名:

SELECT ods.*, bl.*
  FROM    (SELECT aods.user_id,
                  aods.firstname,
                  aods.lastname,
                  Aods.DEPARTMENT,
                  cods.jc_name
             FROM    personas aods
                  LEFT JOIN
                     user_jc cods
                  ON aods.user_id = cods.user_id
            WHERE cods.jc_name LIKE '%ADM%') ods
       FULL OUTER JOIN
          (SELECT abl.user_id,
                  abl.firstname,
                  abl.lastname,
                  Abl.DEPARTMENT,
                  bbl.ug_name
             FROM    personas abl
                  LEFT JOIN
                     ru_ug bbl
                  ON abl.user_id = bbl.user_id
            WHERE Bbl.UG_NAME LIKE '%ADM%'
                  AND bbl.rss_name = 'TIR') bl
       ON ods.user_id = bl.user_id

也许你想要UNION如下[或UNION ALL]:

  SELECT aods.user_id,
          aods.firstname,
          aods.lastname,
          Aods.DEPARTMENT,
          cods.jc_name
     FROM    personas aods
          LEFT JOIN
             user_jc cods
          ON aods.user_id = cods.user_id
    WHERE cods.jc_name LIKE '%ADM%' 

UNION        SELECT abl.user_id,               abl.firstname,               abl.lastname,               Abl.DEPARTMENT,               bbl.ug_name          来自人物角色               LEFT JOIN                  ru_ug bbl               ON abl.user_id = bbl.user_id         在哪里Bbl.UG_NAME喜欢'%ADM%'               AND bbl.rss_name ='TIR'