简体   繁体   English

使用 XMLTABLE 的 LEFT OUTER JOIN 不起作用?

[英]LEFT OUTER JOIN with XMLTABLE doesn't work?

Here is the sample query-这是示例查询-

WITH empdata AS (SELECT xmltype ('<office>
                            <emp>
                                <empno>1</empno>
                                <ename>Abraham</ename>
                                <deptno>10</deptno>
                            </emp>
                            <emp>
                                <empno>2</empno>
                                <ename>Alexander</ename>
                                <deptno>10</deptno>
                            </emp>
                            <emp>
                                <empno>3</empno>
                                <ename>Benjamin</ename>
                                <deptno>20</deptno>
                            </emp>
                            <emp>
                                <empno>4</empno>
                                <ename>Bradley</ename>
                                <deptno>20</deptno>
                            </emp>
                         </office>') AS xcol FROM dual),
     dept AS
         (SELECT 10 deptno, 'Accounting' dname FROM dual
          UNION ALL
          SELECT 20, 'Broking' FROM dual
          UNION ALL
          SELECT 30, 'HR' FROM dual)
SELECT d.dname, e.ename, e.empno
  FROM dept d
       CROSS JOIN empdata e_data
       LEFT OUTER JOIN
       xmltable (
           'office/emp'
           PASSING e_data.xcol
           COLUMNS deptno NUMBER (28, 0) PATH 'deptno',
                   ename VARCHAR2 (10) PATH 'ename',
                   empno NUMBER (28, 0) PATH 'empno') e
           ON d.deptno = e.deptno;

Result I'm getting-结果我得到-

DNAME      ENAME           EMPNO
---------- ---------- ----------
Accounting Abraham             1
Accounting Alexander           2
Broking    Benjamin            3
Broking    Bradley             4

Why isn't the third row from dept , ie that of HR isn't showing in the result set?为什么不是dept的第三行,即 HR 的行没有显示在结果集中? Ideally according to the rules of a LEFT JOIN all the records from the table in the left should show.理想情况下,根据 LEFT JOIN 的规则,应该显示左侧表中的所有记录。 Why is that one being filtered out?为什么要过滤掉那个?

You have the outer join and cross join the wrong way round.您以错误的方式进行了外部连接和交叉连接。 You need to cross-join empdata to xmltable as a subquery, and use that subquery (inline view) as the target for the outer join:您需要将empdata作为子查询交叉连接到xmltable ,并使用该子查询(内联视图)作为外部连接的目标:

...
SELECT d.dname, e.ename, e.empno
  FROM dept d
       LEFT OUTER JOIN
       (
         SELECT x.*
          FROM empdata e_data
               CROSS JOIN 
               xmltable (
                   'office/emp'
                   PASSING e_data.xcol
                   COLUMNS deptno NUMBER (28, 0) PATH 'deptno',
                           ename VARCHAR2 (10) PATH 'ename',
                           empno NUMBER (28, 0) PATH 'empno') x
        ) e
           ON d.deptno = e.deptno;

DNAME      ENAME           EMPNO
---------- ---------- ----------
Accounting Abraham             1
Accounting Alexander           2
Broking    Benjamin            3
Broking    Bradley             4
HR                              

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM