2017-12-19 8 views
1

は、私が試した結果に既にある夫婦次のクエリSQLの異なるB、A、Bのカップルは私が山を共有するすべての国を取得しようとしています<a href="http://www.semwebtech.org/sqlfrontend/" rel="nofollow noreferrer">http://www.semwebtech.org/sqlfrontend/</a></p> <p>で照会することができモンディアルデータベースを使用してい

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 

問題は、私は、各値のカップル2回、1時間を得ることであるcountryA、countryB、別の時間countryB、countryAどのようにすることができますIグラム彼らはすでに結果にあるので、値の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など)が共有していない山はどうですか?つまり、実際には2つの列が必要ですか?

は、このようなクエリを考えてみましょう:

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; 
関連する問題

 関連する問題