SQL不同b,一对夫妇,当a,b夫妇已经在结果中

问题描述 投票:1回答:2

我正在使用可以在http://www.semwebtech.org/sqlfrontend/查询的mondial数据库

我试图让所有共享一座山的国家,我尝试了以下查询

select distinct country1.name, country2.name
from (select distinct geo1.country as c1, geo2.country as c2
      from geo_mountain geo1 join
           geo_mountain geo2
           on geo1.mountain = geo2.mountain and
              not(geo1.country = geo2.country)
     ) mountain,
     country country1, country country2
where country1.code = mountain.c1 and country2.code = mountain.c2

问题是我每次得到两个值,一次是countryA,countryB,另一次是countryB,countryA我怎样才能摆脱第二对值,因为它们已经在结果中了?

sql oracle distinct
2个回答
1
投票

使用正确的join语法:

select c1.name, c2.name
from (select distinct geo1.country as c1, geo2.country as c2
      from geo_mountain geo1 join
           geo_mountain geo2
           on geo1.mountain = geo2.mountain and
              geo1.country < geo2.country
     ) mountain join
     country c1
     on mountain.c1 = c1.code join
     country c2
     on mountain.c2 = c2.code;

<修复了你的问题。


0
投票

那些不是2个国家或3个国家(例如Mousa Ali或Zapaleri)共享的山脉呢?我的意思是,你真的需要两列,还是......?

考虑这样一个问题:

select m.mountain,
  listagg (c.name, ', ') within group (order by c.name) countries
from geo_mountain m, country c
where m.mountain in (select mountain
                     from geo_mountain
                     group by mountain
                     having count(distinct country) > 1
                    )
  and c.code = m.country
group by m.mountain
order by m.mountain;
© www.soinside.com 2019 - 2024. All rights reserved.