【发布时间】:2014-09-19 11:15:10
【问题描述】:
对于我的家庭作业,我必须从有关世界的信息数据库中编写查询。我的一个查询中有一个错误,我不知道为什么。我遇到问题的查询的问题陈述是:
Find all official languages, the country for which it is spoken, and the percentage of speakers
(percentage of speakers is calculated as percentage spoken times country population divided by 100).
Order results by the total number of speakers with the most popular language first. (238 results)
当我尝试从网站运行查询时遇到的错误是:
Query failed: ERROR: missing FROM-clause entry for table "city" LINE 1: ...kers FROM lab2.country AS
co JOIN lab2.country ON lab2.city.... ^
我为查询编写的代码是:
case 11:
$q = "SELECT name, language, ((pecentage * population)/100) AS
percentage_of_speakers FROM lab2.country AS co JOIN lab2.country ON lab2.city.country_code WHERE
(is_official IS TRUE) ORDER BY percentage_of_speakers DESC";
$result = pg_query($q) or die ('Query failed: '. pg_last_error());
break;
我为此查询获得的信息来自两个不同的表,而不是一个。我相信我必须使用 JOIN 语句才能从两个表中获取数据。这是正在使用的 2 个表。提前感谢您的帮助。
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
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
【问题讨论】:
-
感谢您对问题进行了适当的尝试。 (Another student in the same course posted this rather poorer attempt earlier)。不过,以后在寻求家庭作业方面的帮助时,请说这是家庭作业。
标签: php postgresql