Mysql触发并插入值

时间:2017-12-03 18:58:17

标签: php mysql

我尝试在将值插入表ec3_checking之前设置触发器,触发器的功能设置为' account_id'到' CH-n'如果id_count的最高值是(n-1),则id_count是auto_increment参数。

delimiter //
CREATE TRIGGER account_id_add BEFORE INSERT ON `ec3_checking`
FOR EACH ROW
BEGIN
        DECLARE anum int;
        DECLARE bnum int;
        SET bnum =  max(id_account);
        SET anum = bnum + 1;
        SET NEW.account_id =  concat('CH-', anum);
END//

然后我尝试在表格中插入一些值

insert into `ec3_checking` (balance, overdraft_limit)
VALUES
('300','50');

有一个错误:1111,无效使用群组功能,有人可以帮帮我吗? 非常感谢你。

1 个答案:

答案 0 :(得分:0)

CREATE TRIGGER `account_id_add` BEFORE INSERT ON `ec3_checking` 
FOR EACH ROW BEGIN
    DECLARE anum int;

    SET @bnum = (SELECT max(id_account) FROM ec3_checking);

    SET anum = @bnum + 1;
    SET NEW.account_name =  concat('CH-', anum);
END