ORA-00907:SQL中的自然连接缺少右括号

时间:2017-05-01 03:00:04

标签: sql oracle

我试图让它在Oracle Apex中工作,并且我不断收到“ORA-00907:错过右括号”错误,我无法弄明白。我真的很感激一些帮助。

SELECT Genre, ROUND(AVG(NumberofStreams)) AS "Average Number of Streams"
FROM (SELECT Artist.Genre, Songs.NumberofStreams, Songs.SongID
  FROM Songs 
  NATURAL JOIN Artist ON Songs.ArtistID = Artist.ArtistID)
GROUP BY Genre;

1 个答案:

答案 0 :(得分:5)

根据Oracle documentation,自然连接用于根据所有匹配的列名连接2个表。

因此,您的查询中的问题是您无法指定应加入的特定列,因为自然连接将基于所有匹配的列名称加入。

因此,在您的情况下,如果您想基于ArtistID加入,并假设这两个表没有任何其他匹配列,请使用Phil在评论中提到的以下内容。

SELECT Genre, ROUND(AVG(NumberofStreams)) AS "Average Number of Streams"
FROM 
(SELECT Artist.Genre, Songs.NumberofStreams, Songs.SongID
  FROM Songs 
  NATURAL JOIN
  Artist 
 )
GROUP BY Genre;

或者,如果有多个匹配的列,但您只想基于ArtistID加入,则可以将USING子句与INNER JOIN

一起使用
SELECT Genre, ROUND(AVG(NumberofStreams)) AS "Average Number of Streams"
FROM 
(SELECT Artist.Genre, Songs.NumberofStreams, Songs.SongID
  FROM Songs 
  INNER JOIN
  Artist 
  USING (ArtistID)
 )
GROUP BY Genre;

但是为了避免混淆,最好将INNER JOINON子句一起使用,正如您可以从查询中看到的那样,哪些列正在连接。它也可以在加入具有不同名称的列时使用。

SELECT Genre, ROUND(AVG(NumberofStreams)) AS "Average Number of Streams"
FROM 
(SELECT Artist.Genre, Songs.NumberofStreams, Songs.SongID
  FROM Songs 
  INNER JOIN
  Artist 
  ON Songs.ArtistID = Artist.ArtistID
 )
GROUP BY Genre;