繁体   English   中英

如何将结果添加到 MySQL 存储过程的结果集中?

[英]How to add results to the resultset of a MySQL stored procedure?

I'm trying to rewrite my code for searching of keywords in text from PHP to MySQL stored procedure because PHP has too low memory_limit and I'm on a shared hosting so I can't change the memory limit.

我需要编码的是这个(aho-corasick 算法):

for every char ch in text do
     do some transitions
     if there is a result for a given state add it to the output (output is position of word in text and keyword)

我不想让任何人编写程序,但我想知道是否可以像我在上面的伪代码中编写的那样得到 append 结果。

注意:我阅读了文档: http://www.peregrinesalon.com/wp-content/uploads/2009/03/mysql-stored-procedures.pdf和 for 循环很容易编程,条件也很容易,状态之间的转换可能很慢,但似乎仍然有可能。

感谢您的回答!

在 SP 中,您可以在临时表中构建结果集并在退出之前随意操作它。 然后调用程序可以 select 从那里得到它想要的东西。 一旦 MySQL session 关闭,临时表也将被清除。

(编辑) http://dev.mysql.com/tech-resources/articles/mysql-storedprocedures.html

CREATE PROCEDURE procedure1
BEGIN
-- create 'results' table
    CREATE TEMPORARY TABLE OUT_TEMP( val0 varchar(20), val1 int);

    DECLARE done INT DEFAULT 0;
    DECLARE a CHAR(16);
    DECLARE b INT;
    DECLARE cur1 CURSOR FOR SELECT val0, val1 FROM <another table>;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

--open table to read from
    OPEN cur1;

    read_loop: LOOP
    FETCH cur1 INTO a, b;
    IF done THEN
        LEAVE read_loop;
    END IF;
    IF <some condition> THEN
        INSERT INTO OUT_TEMP VALUES (a,b);
    ELSE
  -- insert something else
        INSERT INTO OUT_TEMP VALUES (a,b + 10);
    END IF;
END LOOP;

CLOSE cur1;

-- output results 
SELECT * FROM OUT_TEMP;
DROP TEMPORARY TABLE OUT_TEMP;
END

暂无
暂无

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

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