从2个不同的表中获取信息时,对于我的家庭作业我不得不从关于这个世界的信息的数据库编写查询。我在其中一个查询中遇到错误,我无法弄清楚原因。问题陈述了,我有麻烦的查询是:查询错误在PHP中
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
对你有好的尝试解决问题很有帮助。 ([同一课程中的另一名学生较早发布了这种较差的尝试](http://stackoverflow.com/q/25913964/398670))。但是,在将来,当请求帮助做家庭作业时,请说它是作业。 – 2014-09-19 01:06:45