2013-02-28 63 views
1

为什么oracle不能识别这句话?它说“从关键字”没有找到预期的地方。它出什么问题了 ?Oracle选择不同的语法错误

例子:

select distinct a.id = b.id 
from table1 a, table2 b 
where a.column = X and b.column =Y; 

MySQL允许我这样做。那么我应该改变什么?

+1

你期待什么结果呢? – APC 2013-02-28 22:53:30

+0

我期待某种布尔结果,那就是我为什么在那里放置'='符号,并没有使用'a.id,b.id'。 – 2013-02-28 23:00:09

+1

好吧,那就是我的想法。布尔值不是标准的SQL数据类型。 MySQL已经实现了它,但是Oracle没有。 – APC 2013-02-28 23:04:49

回答

3

首先,Oracle没有在SQL逻辑数据类型(有在PL/SQL逻辑数据类型),所以查询不能返回一个布尔值。

你可以做这样的事情

select distinct (case when a.id = b.id 
         then 1 
         else 0 
        end) 
    from table1 a, 
     table2 b 
where a.column = X 
    and b.column = Y; 

这令我非常不可能的,但是,你真的想要做一个笛卡尔乘积table1table2之间只有然后再涂DISTINCT操作。通常,人们错误地将DISTINCT添加到查询时,他们真正想要做的是添加另一个连接条件。我希望,你真正想要

select distinct (case when a.id = b.id 
         then 1 
         else 0 
        end) 
    from table1 a, 
     table2 b 
where a.some_key = b.some_key 
    and a.column = X 
    and b.column = Y; 

一旦你的联接正确定义,你可能不再需要的额外DISTINCT费用。

6

你的问题是,当它在select子句中时,a.id = b.id不是有效的sql。

编辑下面

鉴于你对期待一个布尔结果的评论,也许你正在寻找一个CASE构造。

select case when a.id = b.id then 1 else 0 end BooleanResult 
from tablea a join tableb b on something 
where etc 
0

这里是它

select distinct a.id, b.id 
from table1 a, table2 b 
where a.column = X and b.column =Y; 
0

您还可以使用解码在Oracle

select distinct (decode (a.id,b.id,1,0)) from table1 a, table2 b 
    where a.some_key = b.some_key 
    and a.column = X ;