我想写一个触发器,需要怎么把变量直接做为where条件来使用,触发器如下:
delimiter //
create trigger insert_ggg
after insert on stat_aaa for each row
begin
     select group_where,group_id into @a,@b from stat_aaa where group_id in (select max(group_id) from stat_aaa);
    insert into stat_bbb(uid,group_id) select uid,@b from stat_ccc where @a;
end; //现在的问题是 where @a 不会被正常识别出来,导致插入的数据为空
有大神帮忙解决下么?

解决方案 »

  1.   

    只能 where x=@a; 这格式
      

  2.   

    肯定是能识别的,但是否满足条件跟 @a 的具体具有关系,参考下面的测试
    mysql> set @a=null;
    Query OK, 0 rows affected (0.00 sec)mysql> select * from(select 1 )  stat_ccc where @a;
    Empty set (0.00 sec)mysql> set @a=1;
    Query OK, 0 rows affected (0.00 sec)mysql> select * from(select 1 )  stat_ccc where @a;
    +---+
    | 1 |
    +---+
    | 1 |
    +---+
    1 row in set (0.00 sec)mysql> set @a=0;
    Query OK, 0 rows affected (0.00 sec)mysql> select * from(select 1 )  stat_ccc where @a;
    Empty set (0.00 sec)