2014-09-18 2 views
-5

숙제를위한 마지막 쿼리를 작성하고 있지만 지금 당장 이에 붙어 있습니다. 이 쿼리는 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 개의 다른 테이블에서 올 때 특히이 수학을 수행 할 방법에 대해 머리를 감싸고 있지 않습니다. 사전에 도움을 주셔서 감사합니다. 여기에이 케이스 country_code, 당신은 공통 요인에 그들에 가입해야합니다 두 테이블에서 정보를 얻기 위해이 쿼리

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    | 
+2

'JOIN IN SQL'을 봤습니까? – HddnTHA

+2

@ user3699735 : 당신은 영리하다고 생각합니까? 기존 게시물을 중복으로 찾을 수 없도록 수정 하시겠습니까? http://stackoverflow.com/revisions/25916257/1 – lxg

+1

이 질문은 OP가 "gimme teh codez"라고하기 때문에 주제와는 거리가 먼 것처럼 보입니다. 또한 OP는 이전에이 질문을 게시했습니다. OP가 데이터를 제공하지 않았으며 sqlfiddle을 설정하지 않았습니다. –

답변

0

에 사용하기 위하여려고하고 테이블입니다. 또한 여기에서 수행 한 것처럼 쿼리에서 바로 백분율 계산을 수행 할 수 있습니다. 마지막으로 PostgrSQL에서 앨리어스 된 열을 질의의 순서대로 열 번호를 사용하여 정렬 할 수 있습니다. 여기이 나라 열에 자본 값이 도시의 ID로 가정하지 않는 한 자본을 나타 내기 위해 도시 테이블에 아무것도 없다, 따라서, 2

SELECT `lab2`.`city`.`name`, ((`lab2`.`city`.`population`/`lab2`.`country`.`population`) * 100) AS `Population Percentage` 
FROM `lab2`.`city` 
JOIN `lab2`.`country` 
ON `lab2`.`city`.`country_code` = `lab2`.`country`.`country_code` 
WHERE `lab2`.`city`.`id` = `lab2`.`country`.`capital` 
ORDER BY 2 

한 주 두 번째 열입니다. 올바른지 알지 못하고이를 사용하도록 쿼리를 수정했습니다.

관련 문제