Order number is incremented by the database

Defined functions

-- 订单号
delimiter $$
create function queryNo(pyear int) returns bigint(20)
begin
set @sum = 11;
UPDATE pce_case_no set `value` = `value`+1 where year =pyear;
set @sum = (SELECT value from shop_order_no where year = pyear limit 1);
if (@sum is null) THEN
INSERT INTO `shop_order_no `(year,value) VALUE (pyear, 1);
set @sum = 1;
end if ;
return @sum;
end;

$$
delimiter ;

Order number increased year beginning table

DROP TABLE IF EXISTS `shop_order_no `;
CREATE TABLE `shop_order_no ` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`year` int(10) NOT NULL,
`value` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of shop_order_no 
-- ----------------------------
INSERT INTO `shop_order_no ` VALUES ('1', '2019', '0');
INSERT INTO `shop_order_no ` VALUES ('2', '2020', '0');
INSERT INTO `shop_order_no ` VALUES ('3', '2021', '0');
INSERT INTO `shop_order_no ` VALUES ('4', '2022', '0');
INSERT INTO `shop_order_no ` VALUES ('5', '2023', '0');
INSERT INTO `shop_order_no ` VALUES ('6', '2024', '0');
INSERT INTO `shop_order_no ` VALUES ('7', '2025', '0');
INSERT INTO `pceshop_order_no case_no` VALUES ('8', '2026', '0');
INSERT INTO `shop_order_no ` VALUES ('9', '2027', '0');

Guess you like

Origin www.cnblogs.com/little-tech/p/11512123.html