Mysql选择循环外的临时表

时间:2014-02-25 10:50:30

标签: mysql

BEGIN

    CREATE TEMPORARY TABLE temp_users LIKE users;
    SET @fact_id = FactId;

    SELECT count(*) INTO @CheckExists from users WHERE fact_id = @fact_id;
    WHILE(@CheckExists > 0)
    DO 
    INSERT temp_users SELECT * FROM users WHERE fact_id = @fact_id;

    SELECT fact_id into @fact_id FROM users WHERE fact_id = @fact_id;
    SELECT count(*) INTO @CheckExists from users WHERE fact_id = @fact_id;


    END WHILE;
    SELECT * FROM temp_users;

END

我不明白为什么当我在循环之外创建SELECT * FROM temp_users;时我得到一个空表,但如果我在while循环中进行此查询,我可以得到一些行

0 个答案:

没有答案