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

时间:2017-04-25 10:57:51

标签: mysql

我有一张名为 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 阻止。

但是如果我在if条件中将 OLD.payment_id 更改为 payment_id ,这将有效当COUNT(payment_id)等于1时,它也会转到 if 块(而不是其他块)

我已经尝试了几种不同的方法来改变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

有人可以帮我弄清楚我做错了什么。

2 个答案:

答案 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)

我正在使用错误表来捕获触发器中发生的事情。您应该能够看到每个删除都会在错误表中触发相应的消息。

答案 1 :(得分:0)

最后我发现了我一直在做的错误。它与 " > 1" 如果使用 OLD.payment_id ,则在AFTER DELETE触发器中,该特定ID已被删除,COUNT将与其余ID一起使用。我的目的是获取具有相同ID的记录的计数,包括已被删除的记录。解决方案是使用" > 0"