簡體   English   中英

SQL 選擇城市

[英]SQL SELECT Cities

我有作業來編寫一個 SQL 查詢:

  1. 選擇所有城市的居民(人口)總數大於 400 的國家
  2. 選擇根本沒有建築物的國家名稱。

有一個表定義

http://imgur.com/hLZZ90F

一世)

SELECT c1.Name
FROM Country c1
JOIN City c2 ON (c1.CountryID = c2.CountryID)
 WHERE NOT EXISTS 
       (SELECT *
          FROM City
         WHERE Population < 400)
;

為什么這不正確? 我沒有得到任何記錄。

有幾種方法,這種方法適用於第一種情況:

select *
from Country co
where 400 < all ( 
            select ci.Population 
            from City ci 
            where ci.CountryID = co.CountryID
            )

對於第二種情況:

select *
from Country co
where 0 = (
    select COUNT(1)
    from City ci 
        JOIN Building B ON B.CityID = CI.CityID
    where ci.CountryID = co.CountryID
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM