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


Writing a Query in php that gets information from 2 different tables

我正在为我的家庭作业写最后一个查询,但我现在一直在写。此查询要求我从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.

对于这个问题,我相信我必须得到整个国家的人口,然后得到首都的人口,再将其除以,才能得到首都人口的百分比。我只是无法理解我将如何执行这个数学运算,尤其是当数据来自两个不同的表时。感谢您提前提供的帮助。以下是我将用于此查询的表

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               | 

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