MySQL Generate order number function

Source: Internet
Author: User

DROP TABLE IF EXISTS ' order_seq ';
CREATE TABLE ' Order_seq ' (
' Timestr ' int (one) is not NULL,
' ORDER_SN ' int (one) not NULL
) Engine=innodb DEFAULT Charset=utf8;

DROP FUNCTION Order_seq;


Create function Order_seq ()
Returns integer
Begin
declare v_cnt integer;
declare v_timestr integer;
declare V_value integer;
DECLARE rowcount BIGINT;
Set V_timestr =date_format (now (), '%y%m%d ');
Select ROUND (RAND () *100,0) + 1 into v_cnt;
UPDATE order_seq Set order_sn = Order_sn + v_cnt where timestr = V_timestr;
If Row_count () = 0 Then
INSERT into ODER_SEQ values (V_TIMESTR,V_CNT);
End If;
Select CONCAT (V_timestr,lpad (order_sn,7,0)) into V_value from order_seq where timestr = V_timestr;
return v_value;
End

--Running results

SELECT Order_seq ();

MySQL Generate order number function

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.