简体   繁体   English

带有插入选择和用户变量的MySQL存储过程

[英]MySQL Stored Procedure with Insert Select and user variable

Problem: I have a MySQL stored procedure and I want to reset a user variable to 0 each time the procedure is called, but the variable seems to be remembering its value from each previous run and I cannot initialize it to zero. 问题:我有一个MySQL存储过程,我想在每次调用该过程时将用户变量重置为0,但是该变量似乎正在记住上一次运行时的值,因此我无法将其初始化为零。

Details: I have a data table (called data ) which I read from and then generate records in a 1-to-1 correlation in another table (called results ). 详细信息:我有一个数据表(称为data ),可以从中读取该数据表,然后在另一个表(称为results )中以1对1的相关关系生成记录。 I do this in a stored procedure that utilizes an Insert Select statement. 我在利用插入选择语句的存储过程中执行此操作。 I also use a user variable @mycount which increments with each row. 我还使用了一个用户变量@mycount ,该变量随每行递增。 Here is a simplified version of my code: 这是我的代码的简化版本:

DELIMITER //
CREATE PROCEDURE doSomething
BEGIN
    SET @mycount := 0;
    INSERT INTO results (data_id, mycounter)
        SELECT data.id, (@mycount := @mycount+1)
        FROM data LEFT JOIN results ON data.id = results.data_id
        WHERE ... GROUP BY ...
        HAVING ...
        ORDER BY ...;
END //
DELIMITER ;

Analysis: This almost works as desired except I need @mycount to reset to zero each time it is run and the SET statement above is not achieving that. 分析:这几乎可以@mycount工作,只是我每次运行时都需要@mycount重置为零,并且上面的SET语句无法实现该目的。 When debugging, I can see that the last value of @mycount is always remembered from the previous run of the stored procedure. 调试时,我可以看到在存储过程的上一次运行中始终会记住@mycount的最后一个值。 I've also tried setting it to zero after the insert. 我也尝试在插入后将其设置为零。

Notes: 笔记:

  1. I am using a @ variable to increment and keep track of row number. 我正在使用@变量来递增并跟踪行号。 Perhaps there is another option here? 也许这里还有另一种选择?
  2. The problem seems related to the Having clause. 该问题似乎与Haveing子句有关。 When I run a similar but different query without the having clause, the variable resets no problem. 当我运行类似但不同的查询而没有Have子句时,该变量将不会出现任何问题。

Question: Why can't I set @mycount to zero before running each time? 问题:为什么每次运行前我@mycount设置为零? If it IS resetting and it's just that my Having clause causes an unexpected count, is there some better way to rewrite my SQL? 如果正在重置,并且仅仅是我的Haveing子句导致意外计数,是否有更好的方法重写SQL?

I'm hoping someone has run into something similar, since this seems pretty obscure. 我希望有人遇到类似的情况,因为这似乎很晦涩。

I think you are using session variable(ie; @mycount).Check below site you will get an answer 我认为您正在使用会话变量(即@mycount)。请在下面的网站上查看您的答案

MySQL: @variable vs. variable. MySQL:@variable与变量。 Whats the difference? 有什么不同?

I dont find any reason why your code should not work. 我找不到您的代码不起作用的任何原因。 Anyways, try the below method. 无论如何,请尝试以下方法。

INSERT INTO results (data_id, mycounter)
select a.id, a.cnt from (
select @mycount := 0 from dual
join ( SELECT data.id, (@mycount := @mycount+1) cnt
        FROM data LEFT JOIN results ON data.id = results.data_id
        WHERE ... GROUP BY ...)) a;

In this case, you dont need to specify SET @mycount := 0; 在这种情况下,您无需指定SET @mycount := 0;

In the following SQL Fiddle I can not reproduce the problem you pose, in theory works as expected. 在下面的SQL Fiddle中,我无法重现您提出的问题,理论上可以按预期工作。

UPDATE UPDATE

Try: 尝试:

DELIMITER //

CREATE PROCEDURE doSomething()
BEGIN
    SET @mycount := 0;
    INSERT INTO results (data_id, mycounter)
        SELECT der.id, (@mycount := @mycount + 1)
        FROM (
            SELECT data.id
            FROM data
                LEFT JOIN results ON data.id = results.data_id
            WHERE ...
            GROUP BY ...
            HAVING ...
            ORDER BY ...
        ) der;
END //

DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM