MySQL uses process storage to generate a unique order number

Source: Internet
Author: User

Database tables:

/*Navicat MySQL Data transfersource server:localsource Server version:50640source host:localhost         : 3306Source database:springsecuritytarget Server type:mysqltarget server Version:50640file Encoding : 65001date:2018-08-25 22:10:57*/SETForeign_key_checks=0;-- ------------------------------Table structure for orders-- ----------------------------DROP TABLE IF EXISTS' orders ';CREATE TABLE' orders ' (' OrderNo ' )varchar( -) not NULLCOMMENT'Order Number', ' Producttypes 'varchar( One) not NULLCOMMENT'Product Type', ' OrderNumber 'int( One)DEFAULT NULLCOMMENT'Number of goods', ' UnitPrice 'decimal(Ten,2) unsigned ZerofillDEFAULT NULLCOMMENT'Unit Price', ' Totalprice 'decimal(Ten,2) unsigned ZerofillDEFAULT NULLCOMMENT'Total Price',  PRIMARY KEY(' OrderNo ') USING BTREE) ENGINE=InnoDBDEFAULTCHARSET=UTF8 Row_format=COMPACT;

To create a process store:

CREATE PROCEDURE' Generate ' (inchOrdernamepreChar(2),inchProducttypesVARCHAR( -),inchOrderNumberINT(Ten),inchUnitPricedecimal(Ten,2))  --Enter the current order name and the length of time to output the order numberBEGIN    DECLARENewordernovarchar( -); DECLAREcurrentdateVarCHAR( the) ;--current date, may contain time division seconds  DECLAREMaxnoINT DEFAULT 0;--The last 5 digits of the serial number of the order number of the nearest satisfying condition, such as: SH2013011000002 's maxno=2  DECLAREOldordernoVARCHAR( -)DEFAULT "';--The most recent order number that satisfies the condition    --SELECT Date_format (now (), '%y%m%d%h%i%s ') into currentdate;--14 digits in minutes and seconds    SELECTDate_format (now (),'%y%m%d%h%i') intocurrentdate;--A total of 14 digits at the date of the month            --SUBSTRING () string intercept function  SELECTIfnull (OrderNo,"') intoOldorderno fromordersWHERE SUBSTRING(OrderNo,3, A)=currentdate--Intercept time period     and SUBSTRING(OrderNo,1,2)=Ordernamepre--intercept the current order name  ORDER  byOrderNoDESCLIMIT1;--There are multiple lines that show only the nearest        --determine if you have an order for the current time period  IFOldorderno!= "'  Then       SETMaxno= SUBSTRING(Oldorderno,-5); END IF ; SELECTCONCAT (Ordernamepre, currentdate, Lpad (maxno+ 1),5,'0')) intoNeworderno; --Lpad ((maxno + 1), 5, ' 0 '): if less than 5 bits, the left is filled with 0  INSERT  intoOrders (Orderno,producttypes,ordernumber,unitprice,totalprice)VALUES(Neworderno,producttypes,ordernumber,unitprice,format (OrderNumber*UnitPrice,2)) ;--inserting data into the order tableEND

Call Procedure Store:

Call Generate ("TT", "slacks",2,198.60)

Finally, the data is inserted into the table successfully:

MySQL uses process storage to generate a unique order number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.