我有一个名为 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 block (而不是 else block )
我尝试了几种不同的方法来更改 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)
我正在使用错误表来捕获触发器中发生的情况。您应该能够看到每次删除都会在错误表中触发相应的消息。
关于如果条件未按预期工作,MySQL AFTER DELETE 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43608775/