2
我想從不同的表編寫查詢,但有一個查詢,我有一個問題。我們正在使用PSQL來進行這些查詢。我試圖執行的查詢是:PSQL查詢從2個不同的表獲取信息
對於每個在數據庫中至少有一百個城市的國家,請列出其包含的城市總數。按照城市數量的升序排列結果。
我知道我將不得不使用計數函數來計算城市的數量,但我很困惑如何列出城市的總數。這裏是我已經試過代碼:
SELECT country.name AS name, COUNT(city.name) AS num_cities
FROM what.country
WHERE (num_cities > 100)
ORDER BY num_cities ASC
這裏是我現在用的是兩個表:
Table "what.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
Table "what.city"
Column | Type | Modifiers
--------------+-----------------------+-----------------------------------------
id | integer | not null default nextval('city_id_seq'::regclass)
name | character varying(35) | not null default ''::character varying
country_code | character(3) | not null default ''::bpchar
district | character varying(20) | not null default ''::character varying
population | integer | not null default 0