刚才的问题大家 帮我解决了 现在出现另外一个还是那2个表 T1(id title number)   T2(id tid 。。) tid为外键 关联t1表id现在想在删除t2表记录时  更新t1表number(number为t2表在t1表对应的总数 t2.tid=t1.id ,帖子和帖子回复的关系)
问题的地方在于 t2表在删除时 是批量删除记录  怎么写触发器?

解决方案 »

  1.   

    不知道你的批量删除是什么?刚才的其实完全可以满足你的要求!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>
      

  2.   

    FOR EACH ROW顾名思义。