日期:2014-05-16 浏览次数:20868 次
如果表的ID 不是自增的: root@localhost : test 10:00:21>select * from tb_test; +------+---------------------+--------+--------+ | id | time | value1 | value2 | +------+---------------------+--------+--------+ | 1 | 2012-09-01 09:41:00 | 33.4 | 56.7 | +------+---------------------+--------+--------+ 1 row in set (0.00 sec) root@localhost : test 09:59:37>delimiter $$ root@localhost : test 10:00:17>create trigger insert_tb -> before insert on tb_test -> for each row -> begin -> select max(value1) into @a from tb_test; -> if new.value1 < @a then set new.value1= @a; -> end if; -> end $$ Query OK, 0 rows affected (0.07 sec) root@localhost : test 10:00:36>insert into tb_test values(1,now(),'12.4','56.7'); Query OK, 1 row affected (0.03 sec) root@localhost : test 10:00:51>select * from tb_test; +------+---------------------+--------+--------+ | id | time | value1 | value2 | +------+---------------------+--------+--------+ | 1 | 2012-09-01 09:41:00 | 33.4 | 56.7 | | 1 | 2012-09-01 10:00:51 | 33.4 | 56.7 | +------+---------------------+--------+--------+ 2 rows in set (0.00 sec) root@localhost : test 10:00:54>insert into tb_test values(1,now(),'55.4','56.7'); Query OK, 1 row affected (0.00 sec) root@localhost : test 10:01:03>select * from tb_test; +------+---------------------+--------+--------+ | id | time | value1 | value2 | +------+---------------------+--------+--------+ | 1 | 2012-09-01 09:41:00 | 33.4 | 56.7 | | 1 | 2012-09-01 10:00:51 | 33.4 | 56.7 | | 1 | 2012-09-01 10:01:03 | 55.4 | 56.7 | +------+---------------------+--------+--------+ 3 rows in set (0.00 sec) 是自增的话,修改下赋值这行就可以了。