POSTGRESQL与MYSQL实现分割字符串的方法对比
Jun 07, 2016 pm 03:23 PM实现分割字符串。 DELIMITER $$ CREATE DEFINER=`root`@`%` FUNCTION `func_get_split_string_total`( f_string VARCHAR(1000),f_delimiter VARCHAR(5) ) RETURNS INT(11) BEGIN -- Get the total number of given string. RETURN 1+(LENGTH(f_string) - LENG
实现分割字符串。DELIMITER $$ CREATE DEFINER=`root`@`%` FUNCTION `func_get_split_string_total`( f_string VARCHAR(1000),f_delimiter VARCHAR(5) ) RETURNS INT(11) BEGIN -- Get the total number of given string. RETURN 1+(LENGTH(f_string) - LENGTH(REPLACE(f_string,f_delimiter,''))); END$$ DELIMITER ;
DELIMITER $$ CREATE DEFINER=`root`@`%` FUNCTION `func_get_split_string`( f_string VARCHAR(1000),f_delimiter VARCHAR(5),f_order INT) RETURNS VARCHAR(255) CHARSET utf8 BEGIN -- Get the separated number of given string. DECLARE result VARCHAR(255) DEFAULT ''; SET result = REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(f_string,f_delimiter,f_order)),f_delimiter,1)); RETURN result; END$$ DELIMITER ;
DELIMITER $$ CREATE PROCEDURE `sp_print_result`( IN f_string VARCHAR(1000),IN f_delimiter VARCHAR(5) ) BEGIN -- Get the separated string. DECLARE cnt INT DEFAULT 0; DECLARE i INT DEFAULT 0; SET cnt = func_get_split_string_total(f_string,f_delimiter); DROP TABLE IF EXISTS tmp_print; CREATE TEMPORARY TABLE tmp_print (v_text varchar(200) NOT NULL); WHILE i <pre class="brush:php;toolbar:false"> 我们来执行:
CALL sp_print_result('love,you,hate,number',','); query result v_text love you hate number
t_girl=# SELECT ytt FROM regexp_split_to_table('love,you,hate,number', E',+') AS ytt; ytt -------- love you hate number (4 rows) t_girl=#
第三种,用自带的WITH 语法来实现。
t_girl=# with recursive ytt(f1,f2) as ( values (0,' '::text) union all select f1+1,split_part('love,you,hate,number',',',f1+1) from ytt where f1 < 20 ) select f2 as result from ytt where f1 >=1 and f1 <= length('love,you,hate,number')-length(replace('love,you,hate,number',',',''))+1; result -------- love you hate number (4 rows) Time: 0.742 ms

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP's big data structure processing skills

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone)

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP?

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors)

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases?
