mysql获得自增的下条id的值

需求: 当数据表中存在一个字段需要该条数据中自增长的id值
实现方法:(1)代码中先插入该条数据后,再次修改数据
   (2)在数据库中使用触发器完成
具体实现:实现方法中的第一种方法就不在此实现,以下便是第二种实现方法的SQL语句
   在tid的值由特殊的字符串组成后,需要拼接该自增的id值在其后面,实现如下
1 DROP TRIGGER `set_tid‘;
2 CREATE TRIGGER `set_tid` BEFORE INSERT ON `task_defination` FOR EACH ROW begin   
3     declare auto_id int;
4       set auto_id=  (select Auto_increment FROM information_schema.tables where table_schema = 'database_name' and table_name='table_name');
5      set new.tid = concat(new.tid,auto_id);
6 end;

 

posted @ 2017-07-17 17:24  bie拿发型挑战我  阅读(140)  评论(0编辑  收藏  举报