码迷,mamicode.com
首页 > 数据库 > 详细

mysql生成订单编号函数

时间:2016-06-28 20:30:28      阅读:1218      评论:0      收藏:0      [点我收藏+]

标签:

DROP TABLE IF EXISTS `order_seq`;
CREATE TABLE `order_seq` (
`timestr` int(11) NOT NULL,
`order_sn` int(11) 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;

--运行结果

SELECT order_seq();

mysql生成订单编号函数

标签:

原文地址:http://www.cnblogs.com/working/p/5624666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!