English 中文(简体)
如何将结果添加到MySQL存储过程的结果集中?
原标题:How to add results to the resultset of a MySQL stored procedure?

我正试图将我的代码从PHP重写为MySQL存储过程,以搜索文本中的关键字,因为PHP的内存限制太低,而且我在共享主机上,所以我无法更改内存限制。

我需要编码的是(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)

我不想让任何人写这个过程,但我想知道是否可以像我在上面的伪代码中写的那样附加结果。

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

谢谢你的回答!

最佳回答

在SP中,您可以在临时表中构建结果集,并在退出前根据需要对其进行操作。然后调用过程可以从中选择它想要的内容。一旦MySQL会话关闭,临时表也将被清除。

(EDIT) 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
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签