gpt4 book ai didi

postgresql - 在 postgres 中使用 query_to_xml 进行内联输出

转载 作者:行者123 更新时间:2023-11-29 13:22:16 25 4
gpt4 key购买 nike

在 postgresql 中运行以下查询

select REPLACE(
REPLACE(
REPLACE(
REPLACE(
query_to_xml(
'select 1 "col1",2 "col2",3 "col3"
union all
select 11 "col1",22 "col2",33 "col3"
union all
select 111 "col1",222 "col2",333 "col3"',
true,
false,
''
)::text ,
'< row >',
'< Leaf >'
),
'< /row >',
'< /Leaf >'
),
'< table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >',
'< Tree >'
),
'< /table >',
'< /Tree >'
)

结果(多行)

< Tree >

< Leaf >

< col1 >1< /col1 >

< col2 >2< /col2 >

< col3 >3< /col3 >

< /Leaf >

< Leaf >

< col1 >11< /col1 >

< col2 >22< /col2 >

< col3 >33< /col3 >

< /Leaf >

< Leaf >

< col1 >111< /col1 >

< col2 >222< /col2 >

< col3 >333< /col3 >

< /Leaf >

< /Tree >

必需的(即单行语句)

< Tree >  < Leaf >   < col1 >1< /col1 >   < col2 >2< /col2 >   < col3 >3< /col3 > < /Leaf >  < Leaf >   < col1 >11< /col1 >   < col2 >22< /col2 >   < col3 >33< /col3 > < /Leaf >  < Leaf >   < col1 >111< /col1 >   < col2 >222< /col2 >   < col3 >333< /col3 > < /Leaf >  < /Tree > 

最佳答案

您需要从结果中删除换行符。要删除新行,您可以使用 regexp_replace(column_or_result, E'[\\n\\r]+', ' ', 'g' )

您的查询应如下所示:

select regexp_replace( REPLACE(REPLACE(REPLACE(REPLACE(query_to_xml('select 1 "col1",2 "col2",3 "col3" union all select 11 "col1",22 "col2",33 "col3" union all select 111 "col1",222 "col2",333 "col3"',true,false,'')::text ,'< row >','< Leaf >') ,'< /row >','< /Leaf >') ,'< table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >','< Tree >') ,'< /table >','< /Tree >'), E'[\n\r]+', ' ', 'g' )

关于postgresql - 在 postgres 中使用 query_to_xml 进行内联输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com