0

I'm making one MySql script that break one string field in multiple words.

I need something like the explode function in PHP!

I try the mysql substring_index function but I have to expecify the number of occurences of the one substring. And that is not predictable in one 10.000 row table.

Any sugestion?

this is my actual Stored Procedure:

**CREATE PROCEDURE `extract_words` ()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE result_row CHAR(100);
DECLARE cursor_1 CURSOR FOR SELECT description FROM db_movies;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;


OPEN cursor_1;


REPEAT
  FETCH cursor_1 INTO result_row;
  IF NOT done THEN
    INSERT INTO extrator_words (word) VALUES (result_row);
  END IF;
UNTIL done END REPEAT;


CLOSE cursor_1;

END $$**

Thanks,
Pedro
@pcamacho

2
  • I doubt this is the solution to your actual problem. What are you trying to accomplish in the first place? Commented Aug 20, 2009 at 15:34
  • I want to create one table with all words in the description field, to generate some SEO Reports, But I'm trying to do that all in Database, without using scripts and in the minimum possible number of Queries Commented Aug 20, 2009 at 15:38

1 Answer 1

1
...
SET @currText = textToExpolde;
SET @sepLen   = LENGTH( separator );

WHILE @currText != '' DO

  SET @word     = SUBSTRING_INDEX(@currText, separator, 1);
  SET @currText = SUBSTRING(@currText, LENGTH(@word) + 1 + @sepLen);

  INSERT INTO extractor_words SET word = @word;

END WHILE;
...
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, that was a typo, that should be currText as well (fixed) =)
Thank You Very MUCH!It work but you have small error instead of SET @currText = SUBSTRING(@word, LENGTH(@word) + 1 + @sepLen); is: SET @currText = SUBSTRING(@currText, LENGTH(@word) + 1 + @sepLen);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.