【问题标题】:Creating sql statements to return information from a table创建 sql 语句以从表中返回信息
【发布时间】:2014-11-11 12:24:46
【问题描述】:

我正在创建 sql 查询以从表中返回信息,但我遇到了一个特别的问题。我想返回科罗拉多州的所有城市地区。 查询的实际定义是

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)      |

然后我有一个显示所有城市信息的表格。再一次,我想我将使用 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)      |

我在 sql 中写的代码是

 select a.name10 from tl_2010_us_uac10 as a join tl_2010_us_state10 as b where (b.name10 = 'colorado');

但我收到此错误

LINE 1: ...l_2010_us_uac10 as a join tl_2010_us_state10 as b where (b.n...

gid 是主键

【问题讨论】:

    标签: datatable subquery postgis psql


    【解决方案1】:

    您必须有一个内连接的连接条件。然后根据您的排序要求订购。

    select a.name10 as urban_area
    from tl_2010_us_uac10 as a 
    join tl_2010_us_state10 as b 
         on b.gid = a.gid
    where b.name10 = 'colorado'
    order by a.name10;
    

    【讨论】:

    • 我尝试时返回了 0 行。我使用 psql 会改变什么吗?
    • 我实际上应该使用 postgis
    • 这是标准的 ansi sql,应该适用于大多数数据库。我确实假设 gid 是关系键。如果不是,你怎么知道哪个城市地区在哪个州?会不会是geoid10?
    • 我想我应该使用ST_Contains这个函数。该程序使用空间关系
    • 您需要将 on 子句替换为 st_contains 函数。我不确定我能否提供进一步的帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多