Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

postgresql - Writing query for multiple tables in php

I am writing my last query for my homework, but I am stuck on it right now. This query requires me to take information from 2 tables instead of 1. I am confused on how to get this information from both tables and how to put them together. Here is the description of the query that I am trying to write.

Find the name, independence year, and region of all countries where English is an official language.
Order results by region ascending and alphabetize the results within each region by country name.   
(44 results)

Here are the tables that I am going to use for this query

Table "lab2.country_language"
Column    |         Type          |               Modifiers                
--------------+-----------------------+----------------------------------------
 country_code | character(3)          | not null default ''::bpchar
 language     | character varying(30) | not null default ''::character varying
 is_official  | boolean               | not null default false
 percentage   | real                  | not null default 0::real
Indexes:
 "country_language_pkey" PRIMARY KEY, btree (country_code, language)
Foreign-key constraints:
"country_language_country_code_fkey" FOREIGN KEY (country_code) REFERENCES country(country_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                  | 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mentionned in the above comment, you have to do an SQL (inner) join in this case. So you'll have something like:

SELECT name, indep_year, region 
FROM lab2.country 
JOIN lab2.country_language
ON lab2.country.country_code = lab2.country_language.country_code
WHERE …

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...