我試圖解決這個http://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial 一些任務,在最後一個任務(8號),我寫了一個查詢:爲什麼此查詢不適用於這種情況?
select name, continent from world a
where a.population >
(select 3*max(population) from world b
where b.continent = a.continent)
但此查詢不返回任何行。但工作幾乎相同的查詢(只是在子查詢的末尾添加了一個額外的條件)。但是,這是怎麼回事?即使國家名稱相同,它爲什麼不返回原始數據?
select name, continent from world a
where a.population >
(select 3*max(population) from world b
where b.continent = a.continent and a.name <> b.name)
你不能在子查詢中使用表別名'a' ..你試圖得到什麼輸出? – Manu
實際上,他可以在子查詢的父查詢中使用列。 –
看看在這兩種情況下子查詢中包含的條目,並注意差異, - ) – Sirko