2016-04-12 33 views
0

我想爲MySQL中的Emp表創建唯一標識,如「Emp_0416_1」,「Emp_0416_2」和「Emp_0516_1」,「Emp_0516_2」。mysql中的特定字符串自動增量

這裏

Emp is stand for Employee, 
04 is April month, 
16 is year, 
1 is emp code. 

而且

Emp is stand for Employee, 
05 is May month, 
16 is year, 
1 is emp code. 

表是:

 create table emp (
      id varchar(20), 
      name varchar(20) 
    ); 

當我插入EMP名字,那麼它會自動增加像上面唯一的ID。

+0

它不是難....你嘗試過什麼吧 –

+0

你想這個字符串中的程序或單查詢? –

+0

@Jordan:我不知道這件事。我是mysql新手。 –

回答

0

這裏是您需要的解決方案

 CREATE PROCEDURE `proc_name`(in emp_code int,in emp_name varchar(256),in_date date) 
     BEGIN 
       declare temp_date varchar(256); 
       set temp_date= (select concat(
               if(length(month(in_date))>1,month(in_date),concat(0,month(in_date))),right(year(in_date),2) 
              ) as dateyear 
           ); 

       INSERT into table_name(id,`name`) 
       select concat('emp_',temp_date,'_',emp_code)as emp_id,emp_name; 



     END 

呼叫流程

call proc_name(2,'batman','2016-04-11')