我有一個表稱爲buyer_invoice_payments條件不工作(buyer_inv_id(PK),payment_id(PK),paid_amt)。當從該表中的記錄被刪除,則需要基於payment_id的計數MySQL後,DELETE觸發是否如預期般
下面是MySQL的查詢,以AFTER DELETE觸發器兩個方面來進行操作,
BEGIN
IF ((SELECT COUNT(payment_id) FROM buyer_invoice_payments BIP WHERE BIP.payment_id = OLD.payment_id) > 1) THEN
# a certain operation
ELSE
# a certain operation
END IF;
END
我的問題是,儘管COUNT(payment_id)大於1,但這種情況總是會失敗,這意味着它會轉到其他塊。
但是,如果我改變OLD.payment_id到payment_id在if條件,這將工作但當COUNT(payment_id)等於1,也進入到如果塊(而不是其他塊)
我已經嘗試了幾種不同的方式來改變if條件,但他們沒有工作,這裏是我試過的幾種方法(只有如果條件顯示)
#1
IF ((SELECT COALESCE(COUNT(payment_id),0) FROM buyer_invoice_payments BIP WHERE BIP.payment_id = OLD.payment_id) > 1) THEN
#2
IF (SELECT (COUNT(payment_id) != 1) FROM buyer_invoice_payments BIP WHERE BIP.payment_id = OLD.payment_id) THEN
有人能幫我弄清楚我做錯了什麼。
哪裏是舊錶?你的查詢是錯誤的。在哪裏塊你把OLD.payment但從你之後沒有提到任何表像OLD的東西 –
@RamiFar OLD不是一個表,它是建立在關鍵字在MySQL中,看看這篇文章http://stackoverflow.com/ question/4798768/i-want-a-trigger-to-delete-from-2-tables-in-mysql –
哦,對不起,確定..這不在我的腦海裏 –