while循环中的变量仅保留最后一个值并在临时表中插入最后一个值

时间:2014-02-13 09:29:12

标签: mysql

如果我测试我的以下过程,那么对于我们在上次选择的状态变量值的所有行中选择exists语句。状态变量仅保留其最后的值而不保留中间值。我的手术有什么问题。我无法理解。

DROP PROCEDURE IF EXISTS check_userfollowed_by_fbuser;
DELIMITER $
create procedure check_userfollowed_by_fbuser (
                 IN myArrayOfValue TEXT, 
                 IN leaderID       INT(11)
                 )
BEGIN

DECLARE status TINYINT(1);
DECLARE value VARCHAR(255);
DECLARE pos INT(11);

CREATE TEMPORARY TABLE fbid_exists_result (fbid varchar(255), status tinyint(1))      
 ENGINE=MEMORY;
 WHILE (CHAR_LENGTH(myArrayOfValue) > 0)
 DO
  SET pos=LOCATE(  ',', myArrayOfValue);

  IF pos>0 THEN
  SET value = LEFT(  myArrayOfValue,pos-1 );
  SET myArrayOfValue= SUBSTRING( myArrayOfValue,pos+1);                                                                                
  ELSE
  SET value = myArrayOfValue;
  SET myArrayOfValue='';
  END IF;

  SELECT EXISTS (
                 SELECT 1 
                   FROM users_followings 
                        LEFT JOIN users 
                                  ON users_followings.userID=users.userID 
                  WHERE FacebookID=value 
                    AND LeaderUserID=leaderID 
                  LIMIT 1
                ) 
    INTO status;
  INSERT INTO fbid_exists_result VALUES(value,status); 

END WHILE;
SELECT fbid,
       status 
  FROM fbid_exists_result ; 
DROP TEMPORARY TABLE IF EXISTS fbid_exists_result ;       

END$

0 个答案:

没有答案