2017-04-25 40 views
0

我有一個表稱爲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_idpayment_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 

有人能幫我弄清楚我做錯了什麼。

+0

哪裏是舊錶?你的查詢是錯誤的。在哪裏塊你把OLD.payment但從你之後沒有提到任何表像OLD的東西 –

+0

@RamiFar OLD不是一個表,它是建立在關鍵字在MySQL中,看看這篇文章http://stackoverflow.com/ question/4798768/i-want-a-trigger-to-delete-from-2-tables-in-mysql –

+0

哦,對不起,確定..這不在我的腦海裏 –

回答

0

最後我發現我一直在做的錯誤。它與「> 1」在AFTER DELETE觸發器中,如果使用了OLD.payment_id,則該特定的ID已被刪除,並且COUNT獲得其餘的ID。我的目的是獲得具有相同標識的記錄的數量,包括已被刪除的記錄。解決方法是使用「> 0」

0

我不同意,這是爲什麼。

drop trigger if exists Tafter_aff_purchases; 
delimiter // 
CREATE DEFINER=`root`@`localhost` TRIGGER `Tafter_aff_purchases` AFTER delete ON `aff_purchases` 
FOR EACH ROW 
BEGIN  
    #insert into errors (msg) values (concat('old.id = ',old.id)); 
IF ((SELECT COUNT(id) FROM aff_purchases BIP WHERE BIP.id = OLD.id) > 0) THEN 
     insert into errors (msg) values ('Operation1 carried out'); 
    ELSE 
     insert into errors (msg) values ('Operation2 carried out'); 
    END IF; 
END // 

delimiter ; 

MariaDB [sandbox]> truncate table aff_purchases; 
Query OK, 0 rows affected (0.27 sec) 

MariaDB [sandbox]> insert into aff_purchases 
    -> (id , affiliate_id , order_id , payout , ip_address , date_submitted) values 
    -> (1,1,1,10,null,'2017-01-01'), 
    -> (1,1,1,20,null,'2017-02-01'), 
    -> (1,1,1,30,null,'2017-03-01'); 
Query OK, 3 rows affected (0.02 sec) 
Records: 3 Duplicates: 0 Warnings: 0 

MariaDB [sandbox]> 
MariaDB [sandbox]> truncate table errors; 
Query OK, 0 rows affected (0.27 sec) 

MariaDB [sandbox]> delete from aff_purchases where id = 1 and payout = 10; 
Query OK, 1 row affected (0.02 sec) 

MariaDB [sandbox]> select * from errors; 
+------------------------+----+ 
| msg     | id | 
+------------------------+----+ 
| Operation1 carried out | 1 | 
+------------------------+----+ 
1 row in set (0.00 sec) 

MariaDB [sandbox]> select * from aff_purchases; 
+------+--------------+----------+--------+------------+----------------+ 
| id | affiliate_id | order_id | payout | ip_address | date_submitted | 
+------+--------------+----------+--------+------------+----------------+ 
| 1 |   1 |  1 |  20 | NULL  | 2017-02-01  | 
| 1 |   1 |  1 |  30 | NULL  | 2017-03-01  | 
+------+--------------+----------+--------+------------+----------------+ 
2 rows in set (0.00 sec) 

MariaDB [sandbox]> delete from aff_purchases where id = 1 and payout = 20; 
Query OK, 1 row affected (0.02 sec) 

MariaDB [sandbox]> select * from errors; 
+------------------------+----+ 
| msg     | id | 
+------------------------+----+ 
| Operation1 carried out | 1 | 
| Operation1 carried out | 2 | 
+------------------------+----+ 
2 rows in set (0.00 sec) 

MariaDB [sandbox]> select * from aff_purchases; 
+------+--------------+----------+--------+------------+----------------+ 
| id | affiliate_id | order_id | payout | ip_address | date_submitted | 
+------+--------------+----------+--------+------------+----------------+ 
| 1 |   1 |  1 |  30 | NULL  | 2017-03-01  | 
+------+--------------+----------+--------+------------+----------------+ 
1 row in set (0.00 sec) 

MariaDB [sandbox]> delete from aff_purchases where id = 1 and payout = 30; 
Query OK, 1 row affected (0.03 sec) 

MariaDB [sandbox]> select * from errors; 
+------------------------+----+ 
| msg     | id | 
+------------------------+----+ 
| Operation1 carried out | 1 | 
| Operation1 carried out | 2 | 
| Operation2 carried out | 3 | 
+------------------------+----+ 
3 rows in set (0.00 sec) 

MariaDB [sandbox]> select * from aff_purchases; 
Empty set (0.00 sec) 

我正在使用錯誤表來捕獲觸發器中發生了什麼。您應該能夠看到每個刪除在錯誤表中觸發適當的消息。

相關問題