/*
SQLyog Ultimate v12.09 (64 bit)
MySQL - 5.7.20-log : Database - lianxi
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`lianxi` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `lianxi`;
/*Table structure for table `bianhao` */
DROP TABLE IF EXISTS `bianhao`;
CREATE TABLE `bianhao` (
`sname` varchar(100) NOT NULL COMMENT '项目名字',
`bianhao` int(10) DEFAULT NULL COMMENT '项目编号',
`time` date DEFAULT NULL COMMENT '时间',
`number` bigint(20) DEFAULT NULL COMMENT '使用次数',
PRIMARY KEY (`sname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `bianhao` */
insert into `bianhao`(`sname`,`bianhao`,`time`,`number`) values ('66',11,'2018-01-18',42);
/* Procedure structure for procedure `mk` */
/*!50003 DROP PROCEDURE IF EXISTS `mk` */;
DELIMITER $$
/*!50003 CREATE DEFINER=`skip-grants user`@`skip-grants host` PROCEDURE `mk`(in kname varchar(100),in num int)
BEGIN
/*申请四个变量 后面好拿来判断*/
DECLARE cs DATE;
DECLARE cs1 INT;
DECLARE cs2 INT;
DECLARE cs3 BIGINT;
/*先判断一下bianhao表里面有没有kname这个数据*/
if exists(select * from bianhao where kname=sname) then
/*这个表里面有数据,然后以时间作为标准,把时间提取出来*/
select `time` into cs from bianhao where kname=sname;
/*设置一下表内时间和当前时间*/
set cs1=CONVERT(DATE_FORMAT(cs,'%Y%m%d'),unsigned);/*表内时间*/
SET cs2=CONVERT(DATE_FORMAT(sysdate(),'%Y%m%d'),UNSIGNED);/*当前时间*/
if cs1=cs2 then/*判断一下当前时间和表内时间是否一样*/
/*一样的话设置一下修改number数,number加1,时间也设置成当前时间,怕到时候修改时间不对*/
UPDATE bianhao SET number=number+1,time=sysdate() WHERE kname=sname;
/*提取上一次的最终number数值*/
select number into cs3 from bianhao where kname=sname;
/*打印编号*/
SELECT CONCAT(kname,num,DATE_FORMAT(SYSDATE(),'%Y%m%d'),lpad(cs3,8,0));
elseif cs1>cs2 then/*判断一下当前时间和表内时间*/
/*表内时间小于当前修改number数,number最终数加1,时间也设置成当前时间*/
update bianhao set number=number+1,TIME=SYSDATE() WHERE kname=sname;
SELECT number INTO cs3 FROM bianhao WHERE kname=sname;
SELECT CONCAT(kname,num,DATE_FORMAT(SYSDATE(),'%Y%m%d'),LPAD(cs3,8,0));
else
UPDATE bianhao SET number=number+1,TIME=SYSDATE() WHERE kname=sname;
SELECT number INTO cs3 FROM bianhao WHERE kname=sname;
SELECT CONCAT(kname,num,DATE_FORMAT(SYSDATE(),'%Y%m%d'),LPAD(cs3,8,0));
end if;
else
/*编号表里面没有这个数据,就重新插入一个,作为起始位*/
insert into bianhao values(kname,num,sysdate(),'1');
select concat(kname,num,date_format(SYSDATE(),'%Y%m%d'),LPAD(1,8,0));
end if;
END */$$
DELIMITER ;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
这些都是sqlyog到出的sql语句