简体   繁体   English

如果条件未按预期运行,则MySQL AFTER DELETE触发器

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

I have a table called buyer_invoice_payments ( buyer_inv_id (PK), payment_id (PK), paid_amt ). 我有一个名为Buyer_invoice_payments的表( Buyer_inv_id (PK), payment_id (PK), paid_amt )。 When a record from this table is deleted it is required to carry out two operations based on the count of payment_id 删除该表中的记录后,需要根据payment_id的计数执行两项操作

Here are the MySQL queries for AFTER DELETE TRIGGER, 这是关于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

My problem is, even though the COUNT(payment_id) is more than 1 this condition will always fails, that means it goes to the else block. 我的问题是,即使COUNT(payment_id)大于1,此条件也总是会失败,这意味着它将转到else块。

But if I change the OLD.payment_id to payment_id in the if condition this will work BUT when COUNT(payment_id) is equal to 1 it also goes to the if block (not to the else block) 但是,如果我改变OLD.payment_id在if条件payment_id这将工作, 但是当COUNT(payment_id)等于1,也转到if块(不else块)

I have tried several different ways of changing the if condition but non of them did work, here are the few ways i tried (only the if condition is shown) 我尝试了几种不同的更改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

Can someone help me to figure out what I have done wrong. 有人可以帮我弄清楚我做错了什么。

I don't agree and here's why. 我不同意,这就是原因。

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)

I am using an errors table to capture whats happening in the trigger. 我正在使用错误表来捕获触发器中发生的事情。 You should be able to see that each delete triggers an appropriate msg in the errors table. 您应该能够看到每个删除操作都会在错误表中触发相应的味精。

Finally I found the mistake that I have been doing. 最终,我发现了我一直在做的错误。 It is with the " >1 " In AFTER DELETE trigger if OLD.payment_id is used, that particular id has been already deleted and the COUNT gets with the rest of the ids. 如果使用OLD.payment_id,则在AFTER DELETE触发器中使用“> 1” ,该特定ID已被删除,而COUNT与其余ID一起获得。 My purpose was to get the count of the records having the same id including the one which has been deleted. 我的目的是获取具有相同ID(包括已删除的ID)的记录的计数。 solution is to use " >0 " 解决方案是使用“> 0”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM