2013-07-19 32 views
0

我想使用NOT EXIST與下面的查詢,而不是NOT IN。使用不存在,而不是不在 -

Select * From Currencymaster Where Currencycode Not In ('USD');

我試圖把它寫這樣的:

Select currencycode from currencymaster where not exists (select currencycode from currencymaster where currencycode='USD');

我不知道這是否是正確的。因爲它沒有給我任何結果。

請驗證或引導我重寫它。 感謝

+0

請出示一些示例數據和預期的結果。目前還不清楚你想要做什麼。 – Barmar

+0

您的NOT IN()查詢選擇美元上沒有貨幣代碼的所有行 - 只要沒有貨幣代碼爲USD的單個行,您的NOT EXISTS()查詢就會從currencymaster選擇所有行。 –

回答

3
Select currencycode 
    from currencymaster a 
where not exists (select currencycode 
        from currencymaster b 
        where b.currencycode='USD' 
         and a.currencycode=b.currencycode); 

select currencycode from currencymaster where currencycode='USD'將永遠在你的condtion返回行bcause它總能找到一排,其中currencycode='USD。要使它工作,你得再添和調理檢查whaeater主錶行即一個具有貨幣=」美元'。

如果你只在curency檢查比你可以嘗試這樣的..

Select currencycode 
     from currencymaster WHERE currencycode!='USD' 
+2

您的回答是解決問題是否正確。但是我認爲,僅僅爲了使用'NOT EXISTS'而使用同一個表是不可行的。 – Romesh

+0

@ RJ1990這裏的答案取決於OP如何...但是你可以建議他/她更好的東西... –

+0

更好的事情是使用原始的NOT IN()。這種方法沒有優勢,而且更羅嗦,更復雜,運行速度會更慢。 –