숙제에 대한 마지막 쿼리를 작성하고 있지만 지금 당장 이에 붙어 있습니다. 이 쿼리는 1 대신 2 개의 테이블에서 정보를 가져와야합니다. 두 테이블에서이 정보를 얻는 방법과 함께 사용하는 방법에 대해 혼란 스럽습니다. 다음은 작성하려고하는 쿼리에 대한 설명입니다. 내가 다음, 전국의 인구를 얻을 수도의 인구를 가져온 다음에 살고있는 인구의 percantage을 얻기 위해 그들을 분할해야 할 것입니다 생각이 쿼리 두 테이블에서 정보를 가져 오는 PHP에서 쿼리 작성
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 |
INNER JOIN은 (는) 귀하의 친구입니다. –
전체 답을주지는 않겠지 만 이전 주석 상태처럼 내부 조인을 사용하려고합니다. 도시에서 인구를 가져 와서 국가 인구수로 나누어야하는 평가가 정확합니다. 두 테이블을 조인 할 때 키가되는 별칭을 설정할 수 있습니다 수학 방정식 : SELECT * FROM lab2.city c JOIN lab2.country c2 ON c.country_code = c2.country_code 이제 c2.population이라고하면 국가 인구가됩니다. c.population을 사용하면 도시 인구 값을 사용합니다. – espradley
@espradley는이 모양이 맞습니까? lab2.city FROM SELECT 이름 c.country_code ON lab2.country의 C2 = 내가 하하 계속 시도 할 필요가 그것을 얻을 해달라고 c2.country_code 가입 c는 – user3596818