好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PostgreSQL 字符串拆分与合并案例

我就废话不多说了,大家还是直接看代码吧~

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

with person_name as (

   select c.id,

       array_to_string(array_agg( distinct p.c_name), ' , ' ) as p_name

   from biz_notification_config c

        join biz_notification_person p

          on p.id =

            any (string_to_array(c.persons, ',' ):: int [])

   group by c.id

),

    group_name as (

      select c.id,

         array_to_string(array_agg( distinct g.c_name), ' , ' ) as g_name

      from biz_notification_config c

          join biz_notification_group g

             on g.id = any (string_to_array(c.c_groups, ',' ):: int [])

      group by c.id

    )

select config.*, person_name.p_name, group_name.g_name

from biz_notification_config config

      left join person_name

           on config.id = person_name.id

      left join group_name

           on config.id = group_name.id;

array_to_string(array_agg(distinct g.c_name), ' , ') :将数组转换为 字符串 ,用[,]分隔。(有点类似于Mysql的group_concat()函数)。

array_agg(distinct 想要 合并 的数据) :将想要的数据变成数组。

string_to_array(c.c_groups, ‘,') :将字符串按照[,]分隔成数组。

any (String(varchar)::int[]) :将字符串转换为整形。

id = any(List) :id的值存在于List中,注意List要和id为同种类型。

补充:POSTGRESQL 与MYSQL 实现分割字符串的方法对比

实现分割字符串。

MYSQL版本。 由于MYSQL不支持递归,不支持返回表类型的结果,所以代码比较繁琐。 我用了两个函数以及一个存储过程来实现。

-- 得到分割符的总数。

?

1

2

3

4

5

6

7

8

9

10

11

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 ;

-- 得到具体下表的子字符。

?

1

2

3

4

5

6

7

8

9

10

11

12

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 ;

-- 打印结果。 用临时表来实现。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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 < cnt

  DO

   SET i = i + 1;

   INSERT INTO tmp_print(v_text) VALUES (func_get_split_string(f_string,f_delimiter,i));

  END WHILE;

  SELECT * FROM tmp_print;

 

END $$

 

DELIMITER ;

我们来执行:

?

1

2

3

4

5

6

7

CALL sp_print_result( 'love,you,hate,number' , ',' );

query result

v_text

love

you

hate

number

PostgreSQL 比较灵活, 有以下几种方法来实现。

第一种,普通的分析字符串方法。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

create or replace function split_to_string(

IN f_string text,

IN f_delimiter varchar (10)

) returns setof text as

$ytt$

  declare cnt int ;

  declare i int ;

  declare v_result text;

  begin

    i := 1;

    cnt := length(f_string) - length( replace (f_string,f_delimiter, '' ))+1;

    while i <= cnt

    loop

     v_result := split_part(f_string,f_delimiter,i);

  return next v_result;

     i := i + 1;

    end loop;

 

  end ;

$ytt$ language plpgsql;

结果:

?

1

2

3

4

5

6

7

8

t_girl=# select split_to_string( 'love,you,hate,number' , ',' ) as result;

  result

--------

  love

  you

  hate

  number

(4 rows )

第二种, 用自己带的正则函数来实现。

?

1

2

3

4

5

6

7

8

9

10

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 语法来实现。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/besokuse233/article/details/105806538

查看更多关于PostgreSQL 字符串拆分与合并案例的详细内容...

  阅读:57次