我 现在有一个表user,字段有自增uid,username,suid。  现在执行insert into user(username)values('1111').现在我想要的是一个触发器     是在插入的时候执行
  suid=这个插入的自增uid。delimiter $
CREATE TRIGGER update_user_suid
AFTER INSERT on user
for each ROW
BEGIN
declare lastid int;
select  last_insert_id() into lastid;
set suid = lastid where uid = lastid;
end $我是这样写的 但是会报Unknown system variable 'uid'的错误。

解决方案 »

  1.   

    delimiter $
     CREATE TRIGGER update_user_suid
     AFTER INSERT on user
     for each ROW
     BEGIN
    set new.suid=last_insert_id();
      end $
      

  2.   


    报Updating of NEW row is not allowed in after trigger  这个错误~
      

  3.   

    delimiter $
      CREATE TRIGGER update_user_suid
      before INSERT on user
      for each ROW
      BEGIN
     set new.suid=last_insert_id();
       end $
      

  4.   

    版主大大  这个能加判断条件么 我加了报错 set new.suid=last_insert_id() where new.suid = '';
      

  5.   

    不用WHERE,直接
    set new.suid=last_insert_id();
      

  6.   

    嗯~ 谢谢版主大大哇, 那我想要加一个条件能行么,有的时候会插入一个suid 插入了就不用自增id,就用插入的值,只有没插入的时候才用自增的id。可以么?
      

  7.   

    set new.suid=if(new.suid is null,last_insert_id(),new.suid)
      

  8.   

    谢谢版主,不过这个if有报语法错误,
    delimiter $
    CREATE TRIGGER a
    before INSERT on table
    for each ROW
    BEGIN
    DECLARE maxid int;
    SELECT max(id) into maxid from table;
    if new.suid = '' then
    set new.suid = maxid+1;
    end if;
    end $
    这样能实现。
      

  9.   

    auto_increment 在触发器中无法得到,考虑一下其它变通方法。