mysql 生成編號 mySQL生成使用者編碼

2021-10-18 02:37:38 字數 2353 閱讀 2609

系統中經常有一些編碼需要生成,以下是生成編碼的設計。

下面opc,db,eb生成編碼的時候要求不帶4。

通過呼叫函式的方式如:select f_getmaxid(1)就可以得到對應的編碼。

表設計:

`tagid` int(8) not null comment '識別號',

`maxno` int(11) not null comment '最大號',

`idlen` tinyint(4) not null comment '最大號',

`idpre` varchar(4) not null comment '字首',

`isid` char(1) not null comment '是否是字元id',

primary key (`tagid`)

) engine=myisam default charset=utf8

呼叫函式:

delimiter $$

use `weipansettlementdb0420`$$

drop function if exists `f_getmaxid`$$

create definer=`weipan_jy`@`%` function `f_getmaxid`(p_tagid tinyint) returns varchar(10) charset utf8

modifies sql data

begin

declare v_idlen tinyint; #總長度

declare v_isid char(1); #是否是id

declare v_i int default 1; #迴圈變數

declare v_j int default 1; #迴圈變數

declare v_num int default 1;#排除4的變數

declare v_numlen int;#排除4變數的長度

declare v_index int;#4出現的位置

declare v_zerocnt int; #加零數量

declare v_id varchar(10); #返回的id;

declare v_idpre varchar(4);#字首

update t_sys_maxid set maxno=1+@mno:=maxno,isid=@isid:=isid

where tagid=p_tagid;

select idpre into v_idpre

from t_sys_maxid where tagid=p_tagid;

if (v_idpre='opc' or v_idpre='db' or v_idpre='eb') then

set v_numlen=length(convert(@mno+1,char(10)));

set v_index=locate('4', convert(@mno+1,char(10)));

if v_index > 0 then

set v_index=v_numlen-v_index;

if v_index > 0 then

while v_j<=v_index do

set v_num=v_num*10;

set v_j=v_j+1;

end while;

end if;

update t_sys_maxid set maxno=maxno+v_num

where tagid=p_tagid;

end if;

end if;

if @isid='y' then

select idpre,idlen,isid into v_id,v_idlen,v_isid

from t_sys_maxid

where tagid=p_tagid;

set v_zerocnt=v_idlen-length(concat(convert(@mno,char(10)),v_id));

while v_i<=v_zerocnt do

set v_id=concat(v_id,'0');

set v_i=v_i+1;

end while;

set v_id=concat(v_id,trim(convert(@mno,char(10))));

else

set v_id=trim(convert(@mno,char(10)));

end if;

return v_id;

end$$

delimiter ;

mySQL 自動生成編號

create table table 1 id int unsigned not null primary keyauto increment,id列為無符號整型,該列值不可以為空,並不可以重複,而且自增。name varchar 5 not null auto increment 100 id列從...

mysql 生成流水號 儲存過程 訂單編號

用儲存過程生成流水號是很常用的,這裡以生成訂單編號的流水號作為示例。新的一天的流水號從1開始,如 今天的訂單編號是cd20130109 00014,下乙個訂單編號將是cd20130109 00015 明天的訂單編號將從cd20130110 00001開始 生成規則 2位字首 年月日 5位流水號 或者...

MySQL使用儲存過程生成編號(親測可用)

create procedure createserial in table name varchar 50 in filed name varchar 50 in ordernamepre varchar 10 in num int,out serial varchar 64 begin decl...