我在SQL中有一个查询,它产生2列坐标:
{lat1,lat2,lat3}和{lon1,lon2,lon3}
以这种方式,相同位置的元素将引用相同的对象(在另一列中再次引用为数组)。
这看起来像这样:
objects latitudes longitudes ---------------------------------------------- {1,2,3} | {lat1,lat2,lat3} | {lon1,lon2,lon3}
我想做的就是让拉/长夫妇这样:
objects coords ---------------------------------------------- {1,2,3} | {{lat1,lon1},{lat2,lon2},{lat3,lon3}}
甚至是:
objects coords ---------------------------------------------- {1,2,3} | {{1,lat1,lon1},{2,lat2,lon2},{3,lat3,lon3}}
如何在postgresql中完成此操作?
答案 0 :(得分:1)
您可以按如下方式使用查询(更改数据类型后):
select
array_cat
(
array_cat
(
array [ [ objects[1], latitutes[1], longitudes[1] ] ]
, array [ objects[2], latitutes[2], longitudes[2] ]
)
, array [ objects[3], latitutes[3], longitudes[3] ]
)
from examplary_table ;
给出了这样的结果:
{{1,111,121},{2,112,122},{3,113,123}}
出于测试目的:
with examplary_table as
(
select * from
(
values
(
'{1,2,3}'::int[]
, '{111,112,113}'::int[]
, '{121,122,123}'::int[]
)
) a (objects, latitutes, longitudes)
)
select
array_cat
(
array_cat
(
array [ [ objects[1], latitutes[1], longitudes[1] ] ]
, array [ objects[2], latitutes[2], longitudes[2] ]
)
, array [ objects[3], latitutes[3], longitudes[3] ]
)
from examplary_table ;
答案 1 :(得分:0)
我认为最简单的方法是阅读它们,进行爆炸,然后将它们重新放回新阵列中。
$def = array( '{1,2,3}', '{lat1,lat2,lat3}', '{lon1,lon2,lon3}' );
$new = array();
$i=0;
foreach( $def as $value ) {
$list = explode( ",", str_replace( array("{", "}"), null, $value ) );
$k=0;
foreach( $list as $item ) {
$new[$k][$i] = $item;
$k++;
}
$i++;
}
输出:
Array
(
[0] => Array
(
[0] => 1
[1] => lat1
[2] => lon1
)
[1] => Array
(
[0] => 2
[1] => lat2
[2] => lon2
)
[2] => Array
(
[0] => 3
[1] => lat3
[2] => lon3
)
)