1

In the query below, why do we use As foo (geom) and not As geom? What does foo() do?

SQL Query

SELECT ST_SRID(geom) AS srid, ST_SRID(ST_SetSRID(geom, 4326)) as srid_new
FROM (
    VALUES (
        ST_GeomFromText('POLYGON((70 20, 71 21, 71 19, 70 20))', 4269)), 
        (ST_Point(1,2)
    )
) As foo (geom);

Using As geom gives the error:

ERROR:  function st_srid(record) does not exist
LINE 1: SELECT ST_SRID(geom) AS srid, ST_SRID(ST_SetSRID(geom, 4326)...
4

1 回答 1

3

As foo is an alias for the subquery, and the (geom) attached to it is the column name in it. SQL requires an alias for every subquery. Find an example in the manual in the chapter Subqueries.

The same goes for a VALUES expression used in this place. I quote the manual here:

Syntactically, VALUES followed by expression lists is treated as equivalent to:

 SELECT select_list FROM table_expression
于 2013-03-30T20:44:45.517 回答