mysql 触发器问题2 刚才的问题大家 帮我解决了 现在出现另外一个还是那2个表 T1(id title number) T2(id tid 。。) tid为外键 关联t1表id现在想在删除t2表记录时 更新t1表number(number为t2表在t1表对应的总数 t2.tid=t1.id ,帖子和帖子回复的关系)问题的地方在于 t2表在删除时 是批量删除记录 怎么写触发器? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不知道你的批量删除是什么?刚才的其实完全可以满足你的要求!mysql> select * from t1;+------+-------+--------+| id | title | number |+------+-------+--------+| 1 | a | 1 || 2 | b | 0 |+------+-------+--------+2 rows in set (0.00 sec)mysql> select * from t2;+------+------+-------+| id | tid | title |+------+------+-------+| 1 | 1 | A |+------+------+-------+1 row in set (0.00 sec)mysql> insert into t2 values -> (2,1,'A'), -> (3,1,'A'), -> (4,1,'A'), -> (5,1,'A'), -> (6,1,'A'), -> (7,2,'B'), -> (8,2,'B'), -> (9,2,'B'), -> (10,2,'B'), -> (11,2,'B');Query OK, 10 rows affected (0.06 sec)Records: 10 Duplicates: 0 Warnings: 0mysql> select * from t1;+------+-------+--------+| id | title | number |+------+-------+--------+| 1 | a | 6 || 2 | b | 5 |+------+-------+--------+2 rows in set (0.00 sec)mysql> select * from t2;+------+------+-------+| id | tid | title |+------+------+-------+| 1 | 1 | A || 2 | 1 | A || 3 | 1 | A || 4 | 1 | A || 5 | 1 | A || 6 | 1 | A || 7 | 2 | B || 8 | 2 | B || 9 | 2 | B || 10 | 2 | B || 11 | 2 | B |+------+------+-------+11 rows in set (0.00 sec)mysql> delete from t2 where id not in (1,7);Query OK, 9 rows affected (0.08 sec)mysql> select * from t1;+------+-------+--------+| id | title | number |+------+-------+--------+| 1 | a | 1 || 2 | b | 1 |+------+-------+--------+2 rows in set (0.00 sec)mysql> select * from t2;+------+------+-------+| id | tid | title |+------+------+-------+| 1 | 1 | A || 7 | 2 | B |+------+------+-------+2 rows in set (0.00 sec)mysql> FOR EACH ROW顾名思义。 大家帮帮忙-大类读多级小类的sql问题 mysql服务器端口分配问题 数据库如何设计,防止一篇文章被同一用户多次顶/踩 mysql主从配置问题 mysql中binary相加的问题 请教 mysqldump 的问题!!! ms-sql里的select top 1在mysql里有什么对应的? MYSQL出现access denied for user 'odbc'@'localhost' (using password:YES) 帮忙看下存储过程是什么意思 any comments 关于sql统计问题 --急 ---100分--- 为什么查询时索引没用上??
+------+-------+--------+
| id | title | number |
+------+-------+--------+
| 1 | a | 1 |
| 2 | b | 0 |
+------+-------+--------+
2 rows in set (0.00 sec)mysql> select * from t2;
+------+------+-------+
| id | tid | title |
+------+------+-------+
| 1 | 1 | A |
+------+------+-------+
1 row in set (0.00 sec)mysql> insert into t2 values
-> (2,1,'A'),
-> (3,1,'A'),
-> (4,1,'A'),
-> (5,1,'A'),
-> (6,1,'A'),
-> (7,2,'B'),
-> (8,2,'B'),
-> (9,2,'B'),
-> (10,2,'B'),
-> (11,2,'B');
Query OK, 10 rows affected (0.06 sec)
Records: 10 Duplicates: 0 Warnings: 0mysql> select * from t1;
+------+-------+--------+
| id | title | number |
+------+-------+--------+
| 1 | a | 6 |
| 2 | b | 5 |
+------+-------+--------+
2 rows in set (0.00 sec)mysql> select * from t2;
+------+------+-------+
| id | tid | title |
+------+------+-------+
| 1 | 1 | A |
| 2 | 1 | A |
| 3 | 1 | A |
| 4 | 1 | A |
| 5 | 1 | A |
| 6 | 1 | A |
| 7 | 2 | B |
| 8 | 2 | B |
| 9 | 2 | B |
| 10 | 2 | B |
| 11 | 2 | B |
+------+------+-------+
11 rows in set (0.00 sec)mysql> delete from t2 where id not in (1,7);
Query OK, 9 rows affected (0.08 sec)mysql> select * from t1;
+------+-------+--------+
| id | title | number |
+------+-------+--------+
| 1 | a | 1 |
| 2 | b | 1 |
+------+-------+--------+
2 rows in set (0.00 sec)mysql> select * from t2;
+------+------+-------+
| id | tid | title |
+------+------+-------+
| 1 | 1 | A |
| 7 | 2 | B |
+------+------+-------+
2 rows in set (0.00 sec)mysql>