3

我的查询:

CREATE VIEW SOME_VIEW2 AS 
(
  SELECT to_char(tbl_albums.album), COUNT(tbl_songs.id) AS "Songs in album"
  FROM tbl_songs 
  LEFT JOIN tbl_albums 
  ON tbl_songs.album_id = tbl_albums.id
  LEFT JOIN tbl_bands
  ON tbl_albums.band_id = tbl_bands.id
  WHERE to_char(LOWER(TRIM(tbl_bands.band))) = 'metallica'
  GROUP BY to_char(tbl_albums.album)
);

我得到的错误:

命令行错误:10 列:12 错误报告:SQL 错误:ORA-00998:必须使用列别名 00998 命名此表达式。00000 -“必须使用列别名命名此表达式”

这不起作用,但是我以前尝试过这个:

CREATE VIEW SOME_VIEW AS 
(
  SELECT * FROM tbl_albums
);

这很好用,想问一下,第一次查询有什么问题。

4

2 回答 2

10

所有列都必须在视图中命名。由于您有一个计算列,即

to_char(tbl_albums.album)

你需要给它一个名字,像这样:

to_char(tbl_albums.album) "Album"
于 2013-10-28T20:11:03.660 回答
0

试试这个查询:

CREATE VIEW SOME_VIEW2 (album , SongsInAlbum) AS 
(
  SELECT q.alb,q.counter FROM
  (SELECT to_char(tbl_albums.album) as alb, rownum as counter
  FROM tbl_songs 
  LEFT JOIN tbl_albums 
  ON tbl_songs.album_id = tbl_albums.id
  LEFT JOIN tbl_bands
  ON tbl_albums.band_id = tbl_bands.id
  WHERE to_char(LOWER(TRIM(tbl_bands.band))) = 'metallica')q
);
于 2013-10-29T07:42:03.853 回答