【发布时间】:2014-09-19 04:21:11
【问题描述】:
我正在为我的作业写最后一个查询,但我现在被困在它上面。此查询要求我从 2 个表而不是 1 个表中获取信息。我对如何从两个表中获取这些信息以及如何将它们放在一起感到困惑。这是我正在尝试编写的查询的描述。
For each country display the capital city name and the percentage of the population that lives in
the capital for each country. Sort the results from largest percentage to smallest percentage.
对于这个查询,我相信我必须得到整个国家的人口,然后得到首都的人口,然后将它们除以得到居住在首都的人口百分比。我只是想不通我将如何执行这个数学运算,尤其是当数据来自 2 个不同的表时。我在这里先向您的帮助表示感谢。这是我将用于此查询的表
Table "lab2.city"
Column | Type | Modifiers
--------------+-----------------------+-----------------------------------------
id | integer | not null default nextval('city_id_seq'::regclass)
name | character varying(35) | not null default ''::character varying
country_code | character(3) | not null default ''::bpchar
district | character varying(20) | not null default ''::character varying
population | integer | not null default 0
Indexes:
"city_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"city_country_code_fkey" FOREIGN KEY (country_code) REFERENCES country(counry_code) ON DELETE CASCADE
=> \d country
Table "lab2.country"
Column | Type | Modifiers
-----------------+-----------------------+--------------------------------------
country_code | character(3) | not null default ''::bpchar
name | character varying(52) | not null default ''::character varying
continent | continent | not null
region | character varying(26) | not null default ''::character varying
surface_area | real | not null default 0::real
indep_year | smallint |
population | integer | not null default 0
life_expectancy | real |
gnp | real |
gnp_old | real |
local_name | character varying(45) | not null default ''::character varying
government_form | character varying(45) | not null default ''::character varying
head_of_state | character varying(60) | default NULL::character varying
capital | integer |
【问题讨论】:
-
你用谷歌搜索了
JOIN IN SQL吗? -
@user3699735:你认为你很聪明,是吗?修改您的旧帖子,使其无法被发现为重复? stackoverflow.com/revisions/25916257/1
-
这个问题似乎是题外话,因为 OP 说“给我 teh codez”。此外,OP 之前已经发布过这个问题。 OP 没有提供数据,也没有设置 sqlfiddle。
-
@user3699735:如果你连一点学习的动力都没有,为什么还要指望别人帮你做作业?你为什么要作弊?
标签: php postgresql