将两行合并为一个具有不同文本排列的行

时间:2017-05-25 12:54:18

标签: mysql

我正在尝试将两行合并为一行,但只有SQL的具体安排

我的第一行是这种格式的地理数据

<Polygon><outerBoundaryIs><LinearRing><coordinates>23.704822,43.139239,0.0 23.704758,43.139544,0.0 23.705024,43.139568,0.0 23.705735,43.1397,0.0 23.706165,43.139754,0.0 23.706769,43.139848,0.0 23.707195,43.13988,0.0 23.70759,43.139877,0.0</coordinates></LinearRing></outerBoundaryIs></Polygon>

第二行

<Point><coordinates>23.724567,43.136242,0.0</coordinates></Point>

问题是如何将这两个行合并为一个格式如此只有MySQL&gt;&gt;&gt;

    <geometry>
<polygon>23.732246,43.132842,0.0 23.732455,43.132991,0.0 23.732673,43.133136,0.0 23.733004,43.133328,0.0 23.733394,43.133522,0.0 23.733649,43.133625,0.0</polygon>
<point>23.724567,43.136242,0.0</point>
</geometry>

我正在使用Workbench 6.3

2 个答案:

答案 0 :(得分:0)

你可以尝试

SELECT 
    concat( '<geometry>',group_concat( col_name SEPARATOR '' ) ,'</geometry>' )

随意使用任何组来调整:) 我认为它有效。

答案 1 :(得分:0)

set @polygonXml = '<Polygon><outerBoundaryIs><LinearRing><coordinates>23.704822,43.139239,0.0 23.704758,43.139544,0.0 23.705024,43.139568,0.0 23.705735,43.1397,0.0 23.706165,43.139754,0.0 23.706769,43.139848,0.0 23.707195,43.13988,0.0 23.70759,43.139877,0.0</coordinates></LinearRing></outerBoundaryIs></Polygon>';
set @pointXml = '<Point><coordinates>23.724567,43.136242,0.0</coordinates></Point>';

set @poligon = ExtractValue(@polygonXml, '/Polygon/outerBoundaryIs/LinearRing/coordinates');
set @point = ExtractValue(@pointXml, '/Point/coordinates');

SELECT CONCAT('<geometry>', '<polygon>', @poligon, '</polygon>', '<point>', @point, '</point>', '</geometry>');

主题是在MySQL 5.1中你有函数ExtractValue。您可以使用您想要的xml数据进行操作。在我的实例xml数据变量中,你也可以从查询中提取它