1

我已经使用create table和这些列创建了一个表:

create table myschema.mytable(
    id serial PRIMARY KEY, 
    row_num integer, 
    col_num integer, 
    pix_centroid geometry, 
    pix_val double precision
)

当我试图填充它时:

insert into pixelbased (id, row_num, col_num, pix_centroid, pix_val)
values (
    DEFAULT, 
    (select((ST_PixelAsPolygons(rast, 1)).x)  from mytable where rid=3),  
    (select((ST_PixelAsPolygons(rast, 1)).x)  from mytable where rid=3), 
    (select(ST_Centroid((ST_PixelAsPolygons(rast, 1)).geom)) from rwanda8 where rid=3),
    (select(ST_PixelAsPolygons(rast, 1)).val from mytable where rid=3)
) 

我遇到以下错误:

错误:用作表达式的子查询返回多行。

我知道,由于我的每一列都有不止一行,所以出现这样的错误是有道理的。但我真的需要按照上述计算所有列。有谁知道我该怎么办?事实上,我想在表中插入以下查询的结果:

select 
    (ST_PixelAsPolygons(rast, 1)).val as geomval1, 
    (ST_PixelAsPolygons(rast, 1)).x as X, 
    (ST_PixelAsPolygons(rast, 1)).y as Y, 
    (ST_Centroid((ST_PixelAsPolygons(rast, 1)).geom)) as geom 
from rwanda8 
where rid=3

有谁知道我该怎么办?

4

2 回答 2

2

您的子查询之一返回超过 1 行。所以使用LIMIT 0,1or 东西来获得每个子查询只有一个值。

如果每列需要超过 1 个值,则应检查插入算法并使用游标。

于 2012-10-30T16:46:07.723 回答
2

只需使用select查询而不是values

insert into pixelbased (row_num, col_num, pix_centroid, pix_val)
select
    (ST_PixelAsPolygons(rast, 1)).val as geomval1,
    (ST_PixelAsPolygons(rast, 1)).x as X,
    (ST_PixelAsPolygons(rast, 1)).y as Y,
    (ST_Centroid((ST_PixelAsPolygons(rast, 1)).geom)) as geom
from rwanda8 where rid=3

不要插入 id,因为它是序列号,会自行生成。

于 2012-10-30T16:49:52.817 回答