2014-01-24 38 views
0

我試圖寫一個小觸發我的數據庫,但我得到的錯誤:的SQLException:您不能指定目標表「P」的更新在FROM子句

的SQLException:你可以用」牛逼指定目標表「p」的更新在FROM子句

我使用的查詢是:

create trigger CheckVendibilitaPP 
after update on hotel 
for each row 
    if (old.IN_VENDITA=true and new.IN_VENDITA=false) then 
    update pacchettopreconfigurato as p 
    set IN_VENDITA=false 
    where p.id_pp in (select p2.id_pp 
         from pacchettopreconfigurato as p2 join hotel as h on p2.DESTINAZIONE_PACCHETTO = h.LUOGO 
         where h.id = new.id) 
       and (select count(*) 
         from pacchettopreconfigurato as p3 join hotel as h2 on p3.DESTINAZIONE_PACCHETTO = h2.LUOGO 
         where p3.id_pp = p.id_pp) > 0; 
end if;; 

我看到一些答案symilar問題,並嘗試了一些的有所示的技術,但沒有那些是有效的。在「SELECT id_pp FROM(select ...)AS c」塊中包裝查詢的第一部分,但那也不起作用。

我該如何重寫查詢以便停止拋出錯誤?

回答

0

至於建議在其他答案中,解決方案是打破子部分中的where子句。 在子查詢中使用參數的需求導致我寫兩個布爾函數分別取參數p.id_pp, new.idp.id_pp。 where子句也因此成了一個簡單的func_1(p.id_pp,new.id) and func_2(p.id_pp)

0

MySQL的文檔說:

Currently, you cannot update a table and select from the same table in a subquery

見倒數第二行here

你可以做的是分別運行這兩個子查詢,將它們的結果放入變量中。然後將該變量替換爲UPDATE語句中的suq-queries。

+0

你不能做到這一點:「......從pacchettopreconfigurato爲P2加入」 ..您剛纔指定的表在FROM子句,因爲錯誤說 –

+0

子查詢不能取參數AFAIK,所以我所做的是使用兩個布爾函數,然後將它們放在更新的where子句中。 – GPhilo

0

我沒有用mysql,所以我不知道,如果它允許你在一個update子句中使用別名,但對於那些數據庫引擎做的,語法是:

update t 
set this = that 
from table t join othertable tt on whatever 
etc 
相關問題