我创建了以下存储过程但是我的Mysql抛出了ERROR 1064。 我需要做的是对于表logbackup中存在的每个用户,找到他的第一个动作的日期,然后是他最后一个动作的日期并计算持续时间。虽然,我知道我可以用不同的方式轻松完成,但是我希望能帮助解决因为我是mysql新手而可能遇到的任何语法错误。
EDIT:
USE 'databasename'
DROP procedure IF EXISTS `userEngagementCalc`;
DELIMITER $$
USE `databasename`$$
CREATE DEFINER=`root`@`%localhost` PROCEDURE `userEngagementCalc`()
CREATE PROCEDURE userEngagementCalc()
BEGIN
DECLARE v_username varchar(50);
DECLARE startDate datetime;
DECLARE endDate datetime;
DECLARE duration int;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 cursor for
select distinct username
from logbackup;
DECLARE continue handler for not found set done=true;
OPEN cur1;
igmLoop: LOOP
fetch cur1 into v_username;
if done then leave igmLoop; end if;
set @startDate:=(select STR_TO_DATE(created_date, '%a %b %d %Y %H:%i:%s') from logbackup where username=v_username order by STR_TO_DATE(created_date, '%a %b %d %Y %H:%i:%s') asc LIMIT 1);
set @endDate:=(select STR_TO_DATE(created_date, '%a %b %d %Y %H:%i:%s') from logbackup where username=v_username order by STR_TO_DATE(created_date, '%a %b %d %Y %H:%i:%s') desc LIMIT 1);
set @duration:= TIME_TO_SEC(TIMEDIFF(@startDate,@endDate));
INSERT INTO userengagement (username,startDate,endDate,duration) VALUES (v_username,@startDate,@endDate,@duration);
END LOOP igmLoop;
CLOSE cur1;
END$$
DELIMITER;
我正在尝试在Ubuntu mysql终端上执行此scipt。是否需要任何特殊操作>? 我从mysql收到的内容除了ERROR消息之外的内容如下:
Display all 813 possibilities? (y or n)
? DAY_MINUTE INNODB MULTILINESTRINGFROMWKB SESSION_USER VARIANCE
ABS DAY_SECOND INOUT MULTIPOINT SET VARYING
ACOS DEALLOCATE INSENSITIVE MULTIPOINTFROMTEXT SHA VAR_POP
ACTION DEC
感谢您的帮助。