簡體   English   中英

如果條件未按預期運行,則MySQL AFTER DELETE觸發器

[英]MySQL AFTER DELETE trigger if condition is not working as expected

我有一個名為Buyer_invoice_payments的表( Buyer_inv_id (PK), payment_id (PK), paid_amt )。 刪除該表中的記錄后,需要根據payment_id的計數執行兩項操作

這是關於AFTER DELETE TRIGGER的MySQL查詢,

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,此條件也總是會失敗,這意味着它將轉到else塊。

但是,如果我改變OLD.payment_id在if條件payment_id這將工作, 但是當COUNT(payment_id)等於1,也轉到if塊(不else塊)

我嘗試了幾種不同的更改if條件的方法,但沒有一種能起作用,這是我嘗試過的幾種方法(僅顯示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

有人可以幫我弄清楚我做錯了什么。

我不同意,這就是原因。

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)

我正在使用錯誤表來捕獲觸發器中發生的事情。 您應該能夠看到每個刪除操作都會在錯誤表中觸發相應的味精。

最終,我發現了我一直在做的錯誤。 如果使用OLD.payment_id,則在AFTER DELETE觸發器中使用“> 1” ,該特定ID已被刪除,而COUNT與其余ID一起獲得。 我的目的是獲取具有相同ID(包括已刪除的ID)的記錄的計數。 解決方案是使用“> 0”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM