数据库自动生成编号SQL

1、不支持修改编号的生成规则:

select 
	concat('371072',date_format(now(),'%Y'),'1',right(concat('000',ifnull(count(1)+1,'')),3)) AS "plantNumber" 
from 
    scjg_spot_check_plan a 

oracle版:

select 
'部门编号'||to_char(sysdate,'yyyyMMdd')|| substr(concat('000000',nvl(count(1)+1,'')),length(count(1)+1)+1) as dasda
from wx_record_query

 

2、支持用户修改编号的生成规则:

select 
			concat('371072',date_format(now(),'%Y'),'1',right(concat('000',ifnull(count(1)+1,'')),3)) AS "plantNumber" 
		from 
			scjg_spot_check_plan a 

union
select 
concat('371072',date_format(now(),'%Y'),'1',right(concat('000',ifnull(max(right(plant_number,3))+1,'1')),3)) 
		from 
			scjg_spot_check_plan a

如果用户修改编号,当前SQL语句会返回2条结果,选择第二条即可.

你可能感兴趣的:(java,算法,SQL)