2013-02-28 36 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 ;