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