extractValue SQL在XML中的同一列中重新调整多个值,后跟空格



我使用的是MySQL数据库,下面是表mytable的属性列中的示例XML。我使用extractValue从XML节点中提取值,这些值在一行中返回,并用空格分隔。我希望结果在同一列的不同行中。

mytable中的列属性包含我需要查询的以下XML。

<Attributes>
<Map>
<entry key="ABC">
<value>
<List>
<String>12 3</String>          
<String>4 56</String>    
</List>
</value>
</entry>
</Map>
</Attributes>

我使用extractValue函数从XML节点中提取值

SELECT itemno, extractValue(attributes, '/Attributes/Map/entry[1]/value/List/String') 
as Value FROM mytable 

带有上述SQL的输出正在返回:

itemno    Value
------    -----
1         12 3  45 6
and I want it as below
itemno    Value
------    -----
1         12 3
1         45 6

我怎样才能做到这一点?

请尝试以下解决方案。

SQL

-- DDL and sample data population, start
CREATE TABLE tbl (ID INT, attributes TEXT);
INSERT INTO tbl VALUES
(1, '<Attributes>
<Map>
<entry key="ABC">
<value>
<List>
<String>12 3</String>
<String>4 56</String>
</List>
</value>
</entry>
</Map>
</Attributes>');
-- DDL and sample data population, end
SELECT ID, ExtractValue(attributes,
'/Attributes/Map/entry[1]/value/List/String[1]/text()') as tokens
FROM tbl
UNION ALL
SELECT ID, ExtractValue(attributes,
'/Attributes/Map/entry[1]/value/List/String[2]/text()') as tokens
FROM tbl;

最新更新