5

我正在尝试运行在 select 语句中使用子查询的查询。
这有效:

select   
(select sysdate from dual),   
(select 'municipality' from dual),  
(select count(*) from municipality)  
from dual;  

但是生成的列名很难看,因此我想添加列别名。

我正在尝试运行以下查询:

select   
(select sysdate from dual) as sysdate,   
(select 'municipality' from dual) as tablename,  
(select count(*) from municipality) as count  
from dual;

这失败了 ORA 00923: From keyword not found where expected 错误。不知何故,我错过了一些东西。

有什么建议么?

谢谢。

4

2 回答 2

2

尝试用双引号括起来,

select
   (select sysdate from dual) as "sysdate",
   (select 'municipality' from dual) as "tablename",
   (select count(*) from municipality) as "count"
from dual;

SysDate是保留关键字。虽然,COUNT是一个聚合函数,但它是允许使用的。

于 2013-02-14T13:29:01.527 回答
1

您在代码中遇到的唯一错误是使用关键字 sysdate 和 count 作为别名,这是不行的……使用 AS 也是可选的。这按预期工作:

select   
 (select sysdate from dual)  s_date,            -- NOT sysdate
 (select 'municipality' from dual)  tablename,  
 (select count(*) from dual) as cnt             -- NOT count
 from dual;

使用计数是可以的,但在现实世界中这是不可取的,专业人士不会使用关键字。

于 2013-02-14T14:20:57.543 回答