[英]Join two select statements sql
I have two sql statements that I wish to join via natural join but for some reason the following is giving me an error: 我有两个希望通过自然连接加入的sql语句,但是由于某些原因,以下内容给了我一个错误:
(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';)
I am using the oracle platform and the error it is throwing is: 我正在使用oracle平台,它抛出的错误是:
natural join
*
ERROR at line 7:
ORA-00933: SQL command not properly ended
What would be the reason this error would be appearing? 出现此错误的原因是什么? Any help would be greatly appreciated.
任何帮助将不胜感激。
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'))
I've removed ;
我已经搬走了
;
and added outer query. 并添加了外部查询。 I would also recommend to replace
natural join
by explicit condition for join
我还建议用明确的
join
条件替换natural join
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';
Without with
it will look like: 如果没有
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';
First, unlearn natural join
. 首先,不学习
natural join
。 It is an error waiting to happen. 这是一个等待发生的错误。 Not showing the
join
keys in the code is dangerous. 在代码中不显示
join
键很危险。 Ignoring declared foreign key relationships is unwise. 忽略声明的外键关系是不明智的。 Relying on naming conventions is awkward.
依赖命名约定很尴尬。
You can write this using using
. 您可以通过写这个
using
。 So, fixing the syntax, this looks like: 因此,修复语法,如下所示:
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'
) cc join
(select city_name
from city left join
country
on country.country_name = city.country_name
where country.continent = 'Europe' and city.iscapitol='no'
) cnc
using (city_name);
Note that the left join
s in the subquery are unnecessary. 请注意,子查询中的
left join
s是不必要的。
That said, I think aggregation is a much simpler approach to the query: 就是说,我认为聚合是一种更简单的查询方法:
select city_name
from city join
country
on country.country_name = city.country_name
where country.continent = 'Europe'
having sum(case when city.iscapitol = 'yes' then 1 else 0 end) > 0 and
sum(case when city.iscapitol = 'no' then 1 else 0 end) > 0;
Or, if iscapitol
[sic] only takes on two values, you can use this for the having
clause: 或者,如果
iscapitol
[原文]只需要两个值,您可以使用此为having
条款:
having min(city.iscapitol) <> max(city.iscapitol)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.