2016-07-06 89 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语句查询多个表。 –