在php中编写一个查询,从2个不同的表中获取信息

时间:2014-09-18 15:04:43

标签: php postgresql

我正在写我的作业的最后一个问题,但我现在仍然坚持下去。这个查询要求我从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               | 

1 个答案:

答案 0 :(得分:1)

您可以在使用连接语句时选择所有需要的列。

相关问题