I have absolutely solved it,just take a look..
At first you have to take a sample table where the columns will be same to the columns of your original table except the column project_id
.
then first insert a row in the original table where the value of column project_id
=0 and the other columns are null,just insert the first row manually like this.
Then create a trigger on the sample table like the following...
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
delimiter ;;
create trigger t after insert on try
for each row
begin
declare v int;
declare c int;
set v=(select max(project_id
) from original
);
if(v=0)then
insert into original
set
project_id
=concat((select concat(right(substring_index((select * from(select curdate() from try
limit 1) as a),'-','1'),2),right(substring_index((select * from(select curdate() from try
limit 1) as a),'-','2'),2)) from try
limit 1),'-001'),
project
=new.project
;
else
set c=(select right((select max(project_id
) from original
)as x,3) from original
limit 1);
insert into original
set
project_id
=concat((select concat(right(substring_index((select * from(select curdate() from try
limit 1) as a),'-','1'),2),right(substring_index((select * from(select curdate() from try
limit 1) as a),'-','2'),2)) from try
limit 1),concat('-00',c+1)),
project
=new.project
;
delete from original
limit 1;
end if;
end;;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
In the above trigger my sample table was try
(project
text) and the original table was original
(project_id
text,project
text).
After creating a trigger like this on the sample table,start inserting rows in the sample table,the rows will automatically inserted in the original table with auto_increment values in the project_id
column like..1405-001,1405-002,1405-003.... where 14
is 2014
and 05
is May
and the rest are auto_incremented values which is being incremented using the trigger.
Just follow the above steps, your problem will be surely solved.