简体   繁体   中英

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 ). When a record from this table is deleted it is required to carry out two operations based on the count of payment_id

Here are the MySQL queries for AFTER DELETE TRIGGER,

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.

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)

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)

#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. My purpose was to get the count of the records having the same id including the one which has been deleted. solution is to use " >0 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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