Вы можете использовать следующий код, чтобы получить желаемый результат ... Приветствия ...
drop table if exists Custom_id_Dynamic;
create table Custom_id_Dynamic (id int primary key auto_increment , Code varchar(20),
Name varchar(20), Reference varchar(20));
drop procedure if exists Dynamic_id;
set @index:=0;
set @jm:=0;
set @unknown:=0;
set @am:=0;
set @af:=0;
set @jf:=0;
delimiter $$
create procedure Dynamic_id(
in code_new varchar(20), in name_new varchar(20)
)
begin
set code_new=lower(code_new);
if code_new='jm' then set @jm:=@jm+1, @index:=@jm;
elseif code_new='am' then set @am:=@am+1, @index:=@am;
elseif code_new='af' then set @af:=@af+1, @index:=@af;
elseif code_new='jf' then set @jf:=@jf+1, @index:=@jf;
else set @unknown:=@unknown+1, @index:=@unknown;
end if;
insert into Custom_id_Dynamic (Code, Name, Reference)
values (code_new, name_new, concat(code_new, lpad(@index,3,0)));
end;
call Dynamic_id('jm','john');
call Dynamic_id('am','patrick');
call Dynamic_id('af','jane');
call Dynamic_id('jm','peter');
call Dynamic_id('jm','derrick');
call Dynamic_id('af','mary');
call Dynamic_id('jf','jessica');
select * from Custom_id_Dynamic;