修改存储过程的定义者:
update mysql.proc set DEFINER='usename' WHERE NAME='proc_name' AND db='mydb';
将mysql中以int类型存储的时间以时间字符串的形式展现:
SELECT FROM_UNIXTIME(ROUND(1431387430641 / 1000),'%Y-%m-%d %H:%m:%s');
查询自1970年到timestamp类型的时间的时间戳
select UNIX_TIMESTAMP('2015-05-12 00:00:00');
自定义函数:返回开始时间与结束时间之间的随机时间,返回类型为timestamp
CREATE FUNCTION `GzzRandomDateTime`( starttime TIMESTAMP, endtime TIMESTAMP) RETURNS timestamp begin return DATE_ADD(starttime,INTERVAL FLOOR(RAND()*ABS(UNIX_TIMESTAMP(endtime)-UNIX_TIMESTAMP(starttime))) SECOND); end
自定义函数:概率
CREATE FUNCTION `GzzChance`( n int) RETURNS int(11) BEGIN declare str varchar(20); if(n=1) then set str='1'; elseif(n=2) then set str='1,2'; elseif(n=3) then set str='1,2,3'; elseif(n=4) then set str='1,2,3,4'; elseif(n=5) then set str='1,2,3,4,5'; elseif(n=6) then set str='1,2,3,4,5,6'; elseif(n=7) then set str='1,2,3,4,5,6,7'; elseif(n=8) then set str='1,2,3,4,5,6,7,8'; elseif(n=9) then set str='1,2,3,4,5,6,7,8,9'; elseif(n=10) then set str='1,2,3,4,5,6,7,8,9,10'; else set str=''; end if; return FIND_IN_SET(1+FLOOR(RAND()*10),str); END
自定义函数:获取最近的一个整数小时数
CREATE FUNCTION `GzzLatest`( starttime TIMESTAMP) RETURNS varchar(50) CHARSET utf8 BEGIN return DATE_FORMAT(DATE_ADD(starttime,INTERVAL 1 Hour),'%Y-%m-%d %H:00:00'); END
自定义函数:获取最近一天未下订单的一个随机用户
CREATE FUNCTION `GzzLatest`( starttime TIMESTAMP) RETURNS varchar(50) CHARSET utf8 BEGIN return DATE_FORMAT(DATE_ADD(starttime,INTERVAL 1 Hour),'%Y-%m-%d %H:00:00'); END
存储过程的一个例子
delimiter $$ create procedure gzz_create_tmp_tables() begin declare starttime TIMESTAMP; declare endtime TIMESTAMP; declare createtime TIMESTAMP; declare userId varchar(20); declare userPhone varchar(50); declare roomId INT; declare hotelId INT; declare roomHour INT; declare roomPrice INT; declare hotelAddr varchar(550); declare hotelKeepTime varchar(50); declare _comment varchar(550); #声明游标遍历标志,默认为1 declare flag int(11) default 1; declare orderId INT; declare requestId INT; declare responseId INT; #查询一个时间段内的所有订单的创建时间 declare c cursor for select FROM_UNIXTIME(ROUND(create_time/1000),'%Y-%m-%d %H:%m:%s') as create_time from order_main where FROM_UNIXTIME(ROUND(create_time/1000),'%Y-%m-%d %H:%m:%s') between '2015-05-12 00:00:00' and '2015-06-16 00:00:00'; #遍历完成时设置游标遍历标志为0 declare continue handler for SQLSTATE '02000' set flag =0; open c; fetch c into starttime; while flag=1 do set endtime=DATE_ADD(starttime,INTERVAL 5 MINUTE); set createtime=GzzRandomDateTime(starttime,endtime); set userId=GzzRandomUserId(starttime,DATE_ADD(starttime,INTERVAL -1 Day)); select phone_number into userPhone from user where user_id=userId; select room.id,room.hotel_id,room_prices.hour,room_prices.price,hotel.address,hotel.hour_room_keep_time,hotel.hour_bidding_remark into roomId,hotelId,roomHour,roomPrice,hotelAddr,hotelKeepTime,_comment from link_response_room inner join room on link_response_room.room_id=room.id and room.pay_type=2 inner join hotel on hotel.id=room.hotel_id inner join room_prices on room_prices.room_id=room.id order by RAND() limit 1; insert into order_request(`user_id`,`order_id`,`order_type`,`address`,`request_time`,`flag`) values(userId,-1,'钟点房',hotelAddr,UNIX_TIMESTAMP(createtime)*1000,1); select LAST_INSERT_ID() into requestId; insert into order_response(`order_id`,`hotel_id`,`valid_time`,`live_time`,`comment`,`room_type`,`keep_to_time`,`last_modify_time`, `create_time`,`is_auto_sell`,`flag`) values(-1,hotelId,createtime,createtime,_comment,'钟点房',STR_TO_DATE(hotelKeepTime,'%H:%i'),createtime,createtime,'Y',1); select LAST_INSERT_ID() into responseId; insert into link_response_room(`response_id`,`room_id`,`price`,`last_hour`,`pay_way`,`status`,`flag`) values(responseId,roomId,roomPrice,roomHour,null,'Y',1); insert into order_main(`request_id`,`response_id`,`contact_people`,`contact_phone`,`come_time`,`status`,`create_time`, `last_modify_time`,`hotel_confirm_status`,`flag`) values(requestId,responseId,'N/A',userPhone,GzzLatest(createtime),'hotel_confirm',UNIX_TIMESTAMP(createtime)*1000,createtime,0,1); select LAST_INSERT_ID() into orderId; update order_request set order_id=orderId where request_id=requestId; update order_response set order_id=orderId where id=responseId; fetch c into starttime; end while; close c; end $$ delimiter ;