select * from (
(select city_name
from city
left join country
on country.country_name=city.country_name
where country.continent='Europe'
and city.iscapitol='yes')
natural join
(select city_name
from city
left join country
on country.country_name=city.country_name
where country.continent='Europe'
and city.iscapitol='no'))
我刪除了;
並添加了外部查詢。我還建議通過明確的條件join
with eurcities as (select city_name, iscapitol, country_name from city
left join country on country.country_name=city.country_name
where country.continent='Europe')
select c1.city_name, c2.city_name, c1.country_name
from eurcities c1 inner join eurcities c2 on (c1.country_name = c2.country_name)
where c1.iscapitol = 'yes' and c2.iscapitol = 'no';
更換natural join
沒有with
它看起來像:
select c1.city_name, c2.city_name, c1.country_name
from (select city_name, iscapitol, country_name from city
left join country on country.country_name=city.country_name
where country.continent='Europe') c1
inner join (select city_name, iscapitol, country_name from city
left join country on country.country_name=city.country_name
where country.continent='Europe') c2
on (c1.country_name = c2.country_name)
where c1.iscapitol = 'yes' and c2.iscapitol = 'no';
@Sal請編輯您的問題,並添加您的表格結構和一些示例數據。使用查詢 – Kacper
@Sal爲您提供建議會容易得多,我編輯了答案並添加了建議的查詢。請試試 – Kacper
'with'子句定義稍後在查詢中使用的數據。您可以考慮這個問題,就像僅針對一個查詢定義的視圖一樣。這會準備僅包含歐洲城市的數據。 'c1'和'c2'只是在'with'子句中定義的表的別名。 @Sal – Kacper