2012-08-28 34 views
0

我有以下查詢:替代的存在查詢

update tab1.abc, 
tab1.sbd = (select tab2.abc, tab2.sbd from tab2,tab1 where --some conditions) 
where exists(select tab2.abc, tab2.sbd from tab2,tab1 where --some conditions) 

現在我的任務就是租期,提高此查詢。

我相信刪除存在的條款或結合兩個where子句將有很大幫助。 但如何做到這一點?

PS:存在子句就位,因爲如果select子句返回零行,我希望更新的行數爲零。

+6

請包括確切的查詢。通過過濾' - some條件'我們實際上看不到你的查詢做了什麼以及如何重構它。 – MatBailie

回答

1

JOIN兩個表,而不是EXISTS。類似如下:

UPDATE tab1 
INNER JOIN tab2 ON --some join condition 
SET sbd = --something 
AND abc = --other something 
WHERE --some conditions 
+1

感謝您的答案,但我從三個表中獲取更新的數據,並檢查這些表中的每個表的一些參數,在這種情況下,加入會不會變得非常昂貴? –

0

您可以使用關鍵字

update tab1.abc, 
tab1.sbd = (select tab2.abc, tab2.sbd from tab2,tab1 where --some conditions) 
where something in 
(select tab2.abc, tab2.sbd from tab2,tab1 where --some conditions) 

參考以下鏈接:

IN (TSql)

SQL IN

+0

我不認爲這會更好的表現,因爲我們運行相同的查詢兩次? –