为oracle创建视图时出现语法错误

时间:2013-10-28 19:56:31

标签: sql oracle

我的查询:

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

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

2 个答案:

答案 0 :(得分:10)

必须在视图中命名所有列。因为你有一个计算列,即

to_char(tbl_albums.album)

你需要给它一个名字,如下:

to_char(tbl_albums.album) "Album"

答案 1 :(得分: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
);