0

我有这个查询:

SELECT
    XMLELEMENT("row", XMLATTRIBUTES(productMain.variant as "order"),
        (SELECT XMLELEMENT("attribute", XMLATTRIBUTES(product.id as "id") )
        FROM product where product.variant = productMain.variant
        )
    )

FROM
    (SELECT 
        DISTINCT product.variant 
    FROM 
        product
    WHERE
        product.fk_parent = 12345) productMain

我收到一个错误'invalid identifier productMain.variant'

预期结果:

<row order="1">
    <attribute name="Example1"/>
    <attribute name="Example2"/>
    ...
</row>
<row order="2">
    ...
</row>

product

id | variant | name
_________________________
1  | 1       | Example1
2  | 1       | Example2
3  | 2       | Example3
4  | 3       | Example4
5  | 3       | Example5

...

SQLFIDDLE: http ://www.sqlfiddle.com/#!4/e91a6/2

我发现,Oracle 不会关联嵌套超过一层的子查询。

我如何仍然得到我想要的结果?

4

2 回答 2

0
SELECT
XMLELEMENT("row", XMLATTRIBUTES(productMain.variant as "order"),
    (SELECT XMLELEMENT("attribute", XMLATTRIBUTES(product.id as "id") )
    FROM product,
    (SELECT DISTINCT variant FROM 
    product
    WHERE fk_parent = 12345) as productMain 
    where product.variant = productMain.variant
    )
)

FROM
(SELECT 
    DISTINCT variant 
FROM 
    product
WHERE
    fk_parent = 12345) productMain
于 2013-10-16T08:37:54.427 回答
0

解决方案:

SELECT
XMLELEMENT("row", XMLATTRIBUTES(productMain.variant as "order"),

  (
  SELECT 
    XMLAGG(XMLELEMENT("attribute",XMLATTRIBUTES(product.name as "name"),product.variant)) FROM product 
    where product.fk_parent = 12345
    and product.variant = productmain.variant
  )

).getClobVal()

FROM
(SELECT 
    DISTINCT product.variant 
FROM 
    product
WHERE
    product.fk_parent = 12345) productMain;
于 2013-10-16T09:50:46.090 回答