我正在使用可在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
问题是我得到的每两个值都是2次,一次是 countryA,countryB ,另一次是 countryB,countryA 我怎样才能摆脱第二对夫妇值已经在结果中了吗?
答案 0 :(得分: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;
<
解决了您的问题。
答案 1 :(得分:0)
除了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;