php - mysql - 在表 2 上删除特定行时在表 1 中插入行

标签 php mysql sql

当使用触发器 mysql 在表 2 上删除特定行时出现此错误,我决定在表 1 中插入行:

MySQL said: #1363 - There is no NEW row in on DELETE trigger

我该怎么做?

最佳答案

考虑以下示例并根据您的触发器进行相应更改

mysql> create table test (id int, val varchar(20),date datetime);
Query OK, 0 rows affected (0.09 sec)

mysql> insert into test values (1,'aa',now()),(2,'bb',now()),(3,'cc',now());
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> create table test1 like test;
Query OK, 0 rows affected (0.06 sec)

mysql> delimiter //
mysql> create trigger test_del after delete on test 
    -> for each row
    -> begin
    -> insert into test1 (id,val,date) values (old.id,old.val,old.date);
    -> end ;
    -> //
Query OK, 0 rows affected (0.12 sec)

mysql> delimiter ;
mysql> select * from test ;
+------+------+---------------------+
| id   | val  | date                |
+------+------+---------------------+
|    1 | aa   | 2014-09-15 15:08:13 |
|    2 | bb   | 2014-09-15 15:08:13 |
|    3 | cc   | 2014-09-15 15:08:13 |
+------+------+---------------------+
3 rows in set (0.01 sec)

mysql> select * from test1;
Empty set (0.00 sec)

mysql> delete from test where id = 1 ;
Query OK, 1 row affected (0.03 sec)

mysql> select * from test1 ;
+------+------+---------------------+
| id   | val  | date                |
+------+------+---------------------+
|    1 | aa   | 2014-09-15 15:08:13 |
+------+------+---------------------+
1 row in set (0.00 sec)

关于php - mysql - 在表 2 上删除特定行时在表 1 中插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25844933/

相关文章:

javascript - 删除评论弹出窗口

php - 准备使用选择查询失败的准备语句

mysql - 添加到现有选择脚本的更新脚本 - 如何在一个脚本中选择然后更新>

android - 适用于 Android 的 ASP.NET Web 服务

php - php首字母大写

php - 如何选择一个更大的id

mysql - mysql上需要大于20位的整数怎么办?

php - MySQL 在 codeigniter 中选择格式的日期

c# - SQL参数编辑

sql - 通过 exec SP 执行相同的代码与在查询窗口中执行 SP 代码报告相同的结果,但执行时间不同