SQL列模糊定义了错误

时间:2017-02-17 05:06:37

标签: sql oracle

我必须写一个查询,告诉我已经出演电影并发行专辑的名人:

Select CELEB
From StarredIn
INTERSECT
Select CELEB
From Releases;

然后从这些名人中,我必须计算此查询中每位名人的电影和专辑的数量。以下是我尝试这样做的方法:

Select DISTINCT CELEB, COUNT(MOVIE) AS number_of_movies, COUNT(ALBUM) AS number_of_albums
From StarredIn, Releases
Where CELEB = 
(Select CELEB
From StarredIn
INTERSECT
Select CELEB
From Releases);

当我在oracle中运行时,我收到错误说"列模糊定义。"那我怎么解决这个问题呢?

这是我用过的表格:

CREATE TABLE StarredIn (
  celeb VARCHAR(30), 
  movie VARCHAR(100)
);
CREATE TABLE Releases (
  celeb VARCHAR(30), 
  album VARCHAR(100)
);

2 个答案:

答案 0 :(得分:1)

您可以使用以下查询

SELECT A.CELEB, A.MOVIECOUNT, B.ALBUMCOUNT FROM
(SELECT  S.CELEB, COUNT(S.MOVIE) AS MOVIECOUNT FROM StarredIn S WHERE S.CELEB IN (SELECT  DISTINCT CELEB FROM Releases) GROUP BY S.celeb) A,
(SELECT  R.CELEB, COUNT(R.ALBUM) AS ALBUMCOUNT  FROM Releases R WHERE R.CELEB IN (SELECT  DISTINCT CELEB FROM StarredIn) GROUP BY R.celeb) B
WHERE A.CELEB = B.CELEB

答案 1 :(得分:0)



Select DISTINCT table1.ColumnName, COUNT(table2.ColumnName2) AS number_of_movies, COUNT(table1.ColumnName3) AS number_of_albums
From table1, table2
Where table1.ColumnName = ''