2016-07-06 84 views
-4

我試圖將此查詢轉換爲沒有內部聯接的等價的一個。沒有INNER JOIN的同一查詢?

select c.name, c.area, ai.sum_area 
from 
    country as c 
    inner join (
     select sum(i.area) as sum_area, gi.country 
     from 
      island i 
      inner join 
      geo_island gi on (i.name = gi.island) 
     group by gi.country 
    ) as ai on (c.code = ai.country) 
where (c.area * 0.99) <= ai.sum_area; 

我想學習如何做,我覺得真unconfortable這個說法,但往往是我找到編寫查詢的唯一途徑。

編輯: 我很感激,而不是把-1,有人告訴我這個問題有什麼問題。

+2

你爲什麼想要消除INNER JOIN?你想在這裏解決什麼問題? –

+0

我在這裏查詢沒有問題。 這是正確的。 我只想找到另一種方式來編寫相同的東西,而不使用INNER JOIN。 – user3004162

+0

如果刪除聯接,它將不會是同一個事物 - 它真的不清楚*爲什麼*您想要消除聯接?如果您使用兩個數據標記問題,那麼知道您正在使用哪個數據庫也很方便。 –

回答

0

一種選擇是使用「舊」 - 非ANSI SQL標準的連接語法:

select c.name, c.area, ai.sum_area 
from country as c, 
    (
     select sum(i.area) as sum_area, gi.country 
     from island i, 
     geo_island gi 
     where (i.name = gi.island) 
     group by gi.country 
) as ai 
where (c.area * 0.99) <= ai.sum_area 
    and c.code = ai.country; 

另一種選擇是使用幾個子查詢(相關和不相關)和運營商。

SELECT * FROM (
    select c.name, c.area, 
      (   
       select sum(i.area) as sum_area 
       from island i 
       WHERE i.name IN (SELECT island FROM geo_island) 
        AND i.country = c.code 
      ) as sum_area 

    from country as c 
) x 
where (area * 0.99) <= sum_area; 
+0

雖然 –

+0

這仍然是一個內部連接第二個示例是* not *寫入連接。但是你的問題是什麼?爲什麼你對連接感到「不舒服」?特別是如果你想用單個SQL語句查詢多個表。 –