我已经构建了以下在Sybase引擎上正常运行的查询:
select *,
(select COUNT(*) from myTable where column1 != '' and column2!=column3) as myField1,
(select COUNT(*) from myTable where column1 != '' and column2=column3) as myField2
from
(select column1,column2,column3,column4,column5
from myTable
where column1 != '' and column2 != column3) as SUB_VIRTUAL_TABLE
但是,在Oracle引擎上运行相同的查询会引发以下异常:
ORA-00923: FROM keyword not found where expected
可以请任何人帮忙修复此语法吗? 非常感谢。 P.s。:表和列存在,当引擎是Sybase时,查询确实运行正常。
预期结果如下:
column1 | column2 | column3 | column4 | column5 | myField 1 | myField2
-----------------------------------------------------------------------
A B C D E 12 15
E F G H I 12 15
...
答案 0 :(得分:2)
您不使用Oracle中的as
关键字来对子查询进行别名。您只需指定别名即可。您可能需要在*
select
添加别名
select sub_virtual_table.*,
(select COUNT(*)
from myTable
where column1 != ''
and column2!=column3) as myField1,
(select COUNT(*)
from myTable
where column1 != ''
and column2=column3) as myField2
from (select column1,column2,column3,column4,column5
from myTable
where column1 != ''
and column2 != column3) SUB_VIRTUAL_TABLE
由于Oracle中的varchar2
列永远不能等于或等于空字符串,因此四个column1
谓词没有做任何事情。您可能希望删除它们或执行正确的IS NULL
/ IS NOT NULL
检查。请参阅why Oracle treats the empty string as NULL上的讨论。
答案 1 :(得分:1)
在Oracle中,''
被视为null
,因此条件!= ''
可以像is not null
一样编写。
试试这个:
select SUB_VIRTUAL_TABLE.*,
(select COUNT(*) from myTable where column1 is not null and column2!=column3) as myField1,
(select COUNT(*) from myTable where column1 is not null and column2=column3) as myField2
from (
select column1,column2,column3,column4,column5
from myTable
where column1 is not null and column2 != column3
) SUB_VIRTUAL_TABLE