Postgis函数ST_Contains编写一个sql查询

时间:2014-11-11 15:31:38

标签: sql postgresql postgis

我在编写使用Postgis函数st_contains的查询时遇到了一些问题。我想展示科罗拉多州的所有市区。查询的实际定义在这里。

Return the names (name10) of all urban areas (in alphabetical order) that are entirely contained 
within Colorado. Return the results in alphabetical order. (64 records)

我使用的表是tl_2010_us_state10(它存储状态的信息)。我想我将在这个表中使用name10变量,因为它具有状态的所有名称。

    Table "public.tl_2010_us_state10"
    Column   |            Type             |                            Modifiers                             
 ------------+-----------------------------+-------------------------------------
  gid        | integer                     | not null default 
  region10   | character varying(2)        | 
  division10 | character varying(2)        | 
  statefp10  | character varying(2)        | 
  statens10  | character varying(8)        | 
  geoid10    | character varying(2)        | 
  stusps10   | character varying(2)        | 
  name10     | character varying(100)      |
  lsad10     | character varying(2)        | 
  mtfcc10    | character varying(5)        | 
  funcstat10 | character varying(1)        | 
  aland10    | double precision            | 
  awater10   | double precision            | 
  intptlat10 | character varying(11)       | 
  intptlon10 | character varying(12)       | 
  coords     | geometry(MultiPolygon,4326) | 

 Indexes:
 "tl_2010_us_state10_pkey" PRIMARY KEY, btree (gid)
 "tl_2010_us_state10_coords_gist" gist (coords)

然后我有一张显示所有城市信息的表格。我再一次认为我将使用name10变量,因为它存储了所有城市区域的名称。

                                      Table "public.tl_2010_us_uac10"
    Column   |            Type             |                           Modifiers                         
------------+-----------------------------+-------------------------------------
 gid        | integer                     | not null default 
 uace10     | character varying(5)        | 
 geoid10    | character varying(5)        | 
 name10     | character varying(100)      |
 namelsad10 | character varying(100)      | 
 lsad10     | character varying(2)        | 
 mtfcc10    | character varying(5)        | 
 uatyp10    | character varying(1)        | 
 funcstat10 | character varying(1)        | 
 aland10    | double precision            | 
 awater10   | double precision            | 
 intptlat10 | character varying(11)       | 
 intptlon10 | character varying(12)       | 
 coords     | geometry(MultiPolygon,4326) | 

 Indexes:
"tl_2010_us_uac10_pkey" PRIMARY KEY, btree (gid)
"tl_2010_us_uac10_coords_gist" gist (coords)

我在sql中编写的代码是

 select a.name10 as urban_area
from tl_2010_us_uac10 as a 
join tl_2010_us_state10 as b 
on ST_Contains(b.gid = a.gid)
where b.name10 = 'Colorado'
order by a.name10;

但我收到此错误

ERROR:  function st_contains(integer, integer) does not exist
LINE 1: ...010_us_uac10 as a join tl_2010_us_state10 as b on ST_Contain...
                                                         ^
HINT:  No function matches the given name and argument types. You might need to add explicit 
type casts.

1 个答案:

答案 0 :(得分:0)

ST_Contains需要两个参数,两个参数都应为几何类型。因此你犯了两个错误。

  1. 使用int字段
  2. 将它们与=进行比较,而不是将它们作为单独的参数提供
  3. 通过更正这些来获得

    select a.name10 as urban_area
    from tl_2010_us_uac10 as a 
    join tl_2010_us_state10 as b 
      on ST_Contains(b.coords, a.coords)
    where b.name10 = 'Colorado'
    order by a.name10;