2017-04-25 37 views
0

我有一个表称为buyer_invoice_payments条件不工作(buyer_inv_id(PK),payment_id(PK),paid_amt)。当从该表中的记录被删除,则需要基于payment_id的计数MySQL后,DELETE触发是否如预期般

下面是MySQL的查询,以AFTER DELETE触发器两个方面来进行操作,

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,但这种情况总是会失败,这意味着它会转到其他块。

但是,如果我改变OLD.payment_idpayment_id在if条件,这将工作当COUNT(payment_id)等于1,也进入到如果块(而不是其他块)

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

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

+0

哪里是旧表?你的查询是错误的。在哪里块你把OLD.payment但从你之后没有提到任何表像OLD的东西 –

+0

@RamiFar OLD不是一个表,它是建立在关键字在MySQL中,看看这篇文章http://stackoverflow.com/ question/4798768/i-want-a-trigger-to-delete-from-2-tables-in-mysql –

+0

哦,对不起,确定..这不在我的脑海里 –

回答

0

最后我发现我一直在做的错误。它与“> 1”在AFTER DELETE触发器中,如果使用了OLD.payment_id,则该特定的ID已被删除,并且COUNT获得其余的ID。我的目的是获得具有相同标识的记录的数量,包括已被删除的记录。解决方法是使用“> 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) 

我正在使用错误表来捕获触发器中发生了什么。您应该能够看到每个删除在错误表中触发适当的消息。

相关问题