從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