日期:2014-05-20  浏览次数:20739 次

mysql 触发器 根据更新的这一列的数据来更新本行的另一列数据
求助用触发器实现test表column1值为5时,修改本行数据的column2的值为10,数据库为mysql的,
急求,谢谢各位大虾

------解决方案--------------------
先去熟悉语法吧,这个是最基本的。。
------解决方案--------------------
SQL code
mysql> CREATE TRIGGER tr_newtable_bi BEFORE INSERT ON newtable
    -> FOR EACH ROW
    -> set new.bb=case new.aa when 3 then 5 when 2 then 10 else new.bb end;
Query OK, 0 rows affected (0.17 sec)

mysql> insert into newtable values (1,2,3),(2,3,4),(3,6,9);
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from newtable;
+------+------+------+
| id   | aa   | bb   |
+------+------+------+
|    1 |    2 |   10 |
|    2 |    3 |    5 |
|    3 |    6 |    9 |
+------+------+------+
3 rows in set (0.00 sec)

mysql>