1

自然結合を介して結合したい 2 つの 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';)

私は oracle プラットフォームを使用していますが、スローされているエラーは次のとおりです。

natural join
*
ERROR at line 7:
ORA-00933: SQL command not properly ended

このエラーが表示される理由は何ですか? どんな助けでも大歓迎です。

4

2 に答える 2

1
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'))

;外部クエリを削除して追加しました。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';

それなし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';
于 2016-11-18T20:21:00.937 に答える
0

まず、学習をやめnatural joinます。起こるのを待っているエラーです。コードにキーを表示しないのjoinは危険です。宣言された外部キー関係を無視するのは賢明ではありません。命名規則に依存するのは厄介です。

を使って書くことができますusing。したがって、構文を修正すると、次のようになります。

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);

left joinサブクエリの s は不要であることに注意してください。

そうは言っても、集計はクエリに対するより単純なアプローチだと思います。

      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;

または、iscapitol[sic] が 2 つの値しかとらない場合は、これをhaving句に使用できます。

      having min(city.iscapitol) <> max(city.iscapitol)
于 2016-11-18T20:48:40.143 に答える