將spam_keyword表word字段的字符所有拆分,只是利用過程語言完成循環的操做而已。html
create or replace function proc1() returns setof text as $$ declare str text; strlength int; strreturn text; i int; curs1 refcursor; --聲明遊標 begin open curs1 for select replace(word,' ','') from spam_keyword; --在loop前打開遊標並綁定好範圍,query定義範圍的時候就把空格都替換掉 <<loop1>> --標籤 loop fetch curs1 into str; if not found then exit; end if; i:=0; --每一次遊標完成賦值後就把i初始化爲0 <<loop2>> loop i:=i+1; strreturn:=substring(str,i,1); strlength:=char_length(str); return next strreturn; exit when i>=strlength; end loop loop2; end loop loop1; close curs1; end; $$ language plpgsql
返回的是setof結果集,再用分組查詢能夠統計到各個字符的數量。正則表達式
select proc1,count(*) from proc1() group by proc1 order by count desc;
proc1 | count -------+------- 醫 | 65 院 | 61 小 | 41 1 | 38 州 | 36 0 | 35 5 | 32 ...省略1000行左右 他 | 1 丹 | 1 撲 | 1 朵 | 1 債 | 1 } | 1 (1145 行記錄)
寫成通用的函數,經過指定參數來分割sql
create or replace function split_to_table(str text) returns setof text as $$ declare strlength int; strreturn text; i int; begin i:=0; loop i:=i+1; str:=replace(str,' ',''); strlength:=char_length(str); strreturn:=substring(str,i,1); return next strreturn; exit when i>=strlength; end loop; end; $$ language plpgsql
這樣使用express
select split_to_table(word) from spam_keyword;
後來忽然想起來去看看是否是原本就有分割字符的函數在。。一查果真有。。。席巴。。。數組
函數:regexp_split_to_array(string text, pattern text [, flags text ])
說明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利用正則表達式將字符串分割成數組
例子:regexp_split_to_array('hello world', E'\\s+') = {hello,world}
函數:regexp_split_to_table(string text, pattern text [, flags text])
說明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利用正則表達式將字符串分割成表格
例子:regexp_split_to_table('hello world', E'\\s+') =
hello
world
(2 rows)bash
select regexp_split_to_table(word,E'') from spam_keyword;