开发者

MySQL Split String To Return All Words in All Rows

开发者 https://www.devze.com 2023-03-27 17:48 出处:网络
I have a stored procedure as documented in: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/ to use MySQL\'s Substring() function to get each word in a string.What I want is for the fo

I have a stored procedure as documented in: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/ to use MySQL's Substring() function to get each word in a string. What I want is for the following query to return every word that appears in every row of the Sentences table. Currently, I need to filter through position 1, 2, 3 etc. But I don't know how many words a sentence might have, so that's very inefficient. Any ideas for how to get all words in all sentences in one query?

SELECT DISTINCT SPLIT_STR(`SentenceText`, " ", 1) AS Word FROM `Senten开发者_Python百科ces`;

As an example: If Sentences contained 2 rows:

this is a sentence
sentence galore

I want a single query that returns:

this
is
a
sentence
galore


You need to use the GROUP_CONCAT function to get all sentences in one line:

SELECT GROUP_CONCAT(sentence_column SEPARATOR ' ')
FROM Sentences
GROUP BY sentence_column;

Then you need to tokenize the sentence and insert the tokens in a temporary table and then do the select from there. Here is a good example (I think is just what you need). Good luck!

Update (because of downvotes): Maybe the problem with my answer is that is not a single query, but a three steps process:

  1. Get all the sentences in one single string (using group_concat function)
  2. Pass this string to a procedure (I've put a link to a good example of such a procedure, I don't think copy & paste here adds any value) that tokenizes the string and insert each string in a table, maybe a temporary table (lets call it WORDS).
  3. Execute a simple select like SELECT * FROM WORDS

I think this steps achieve the desired results (but not in one single query)

If you put this three steps in a new stored procedure, then you can do a single query to it.

Please, if you are going to downvote, at least take the time to explain why you are downvoting (as I've taken the time to read the question and answer).


This is the mysql procedure to do the job (tested with mysql 5.5):

DROP PROCEDURE if exists split_sentence;
delimiter $$
CREATE PROCEDURE split_sentence(sentence varchar(255), delimiter VARCHAR(50), out result_wordcount INTEGER)
BEGIN  
  DECLARE last_position INTEGER;
  DECLARE position INTEGER;

  DROP TABLE IF EXISTS tmp_split_sentence;
  CREATE TEMPORARY TABLE tmp_split_sentence (word varchar(255));

  set last_position = 1;
  set position = instr(sentence, delimiter);
--  select position;  

  WHILE position > 0 DO
--      select concat('found: ', substring(sentence, last_position, position-last_position));
      INSERT INTO tmp_split_sentence (word) VALUES(substring(sentence, last_position, position-last_position));

      set last_position = position+length(delimiter);
      set position = locate(delimiter, sentence, last_position);  
--      select concat('position: ', position);
  END WHILE;

-- wrap up to the end of the sentence
  if last_position < length(sentence) then
     INSERT INTO tmp_split_sentence (word) VALUES(substring(sentence, last_position, length(sentence)-last_position+1));
  end if;

  SELECT count(*) into result_wordcount from tmp_split_sentence;

END$$
delimiter ;    
DROP TABLE IF EXISTS tmp_sentence;
CREATE TEMPORARY TABLE tmp_sentence (word varchar(255));
call split_sentence('this is a sentence', ' ', @result_wordcount);
INSERT INTO tmp_sentence select * from tmp_split_sentence;
call split_sentence('sentence galore', ' ', @result_wordcount);
INSERT INTO tmp_sentence select * from tmp_split_sentence;

select * from tmp_sentence;

Result:

this
is
a
sentence
sentence
galore

Note that sentence appears to times, that should be fixed in the question :).

0

精彩评论

暂无评论...
验证码 换一张
取 消