微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在php中编写多个表的查询

我正在写我的作业的最后一个问题,但我现在仍然坚持下去.这个查询要求我从2个表而不是1个表中获取信息.我对如何从两个表中获取这些信息以及如何将它们放在一起感到困惑.以下是我要编写的查询的说明.

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)

以下是我将用于此查询的表

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                  | 

解决方法:

如上面的注释中所提到的,在这种情况下,您必须执行sql(内部)连接.所以你会有类似的东西:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐