DROP FUNCTION IF EXISTS getTimes;

DELIMITER $$

CREATE    FUNCTION  getTimes(idstr VARCHAR(50)) 

RETURNS INT 

BEGIN 

DECLARE times INT; 

DECLARE a INT;

DECLARE tempdt DATETIME;

DECLARE tempdt2 DATETIME; 

SET  times = 0; 

SET a=1;

SELECT COUNT(*) INTO times FROM t_sys_member_sign  WHERE TO_DAYS(NOW())-TO_DAYS(SIGN_TIME)<=0 AND id=idstr;

SELECT SIGN_TIME INTO tempdt FROM t_sys_member_sign WHERE TO_DAYS(NOW())-TO_DAYS(SIGN_TIME)=1  AND id=idstr;

WHILE tempdt IS NOT NULL AND a=1 DO 

SET times=times+1; 

SET tempdt2=tempdt;

SELECT SIGN_TIME INTO tempdt FROM t_sys_member_sign WHERE TO_DAYS(tempdt)-TO_DAYS(SIGN_TIME)=1  AND id=idstr;

IF TO_DAYS(tempdt2)-TO_DAYS(tempdt)<1 THEN SET a=0; END IF;

END WHILE; 

RETURN times;

END$$

SELECT getTimes('009af8fe7c20431994098e044d36b058');