And a combination of cutting function database sql

Table of Contents

A combination of function 

1.concat 

2.concat_ws 

Second, cutting function 

1.split_part 

2.regexp_split_to_table 

3.regexp_split_to_array 

Three: multi-line results are added


A combination of function 


1.concat 


a. grammar Introduction

concat(str "any" [, str "any" [, ...]])

Concatenate all but first arguments with separators.
The first parameter is used as a separator. 
NULL arguments are ignored.

. B practical examples:

postgres=# create table t_kenyon(id int,name varchar(10),remark text);
CREATE TABLE
postgres=# insert into t_kenyon values(1,'test','kenyon'),(2,'just','china'),(3,'iam','lovingU');
INSERT 0 3
postgres=# insert into t_kenyon values(4,'test',null);
INSERT 0 1
postgres=# insert into t_kenyon values(5,null,'adele');
INSERT 0 1
postgres=# select * from t_kenyon;
 id | name | remark  
----+------+---------
  1 | test | kenyon
  2 | just | china
  3 | iam  | lovingU
  4 | test | 
  5 |      | adele
(5 rows)

postgres=# select concat(id,name,remark) from t_kenyon;
   concat    
-------------
 1testkenyon
 2justchina
 3iamlovingU
 4test
 5adele
(5 rows)

c. Description 
concat function is purely a function of splicing, the splicing can ignore the value of a null value, no mosaic delimiter, the delimiter if necessary, you need to use the following function concat_ws. 


2.concat_ws 


a. grammar Introduction

concat_ws(sep text, str "any" [, str "any" [,...] ])

Concatenate all but first arguments with separators.
The first parameter is used as a separator.
NULL arguments are ignored.

b. the practical application

postgres=# select concat_ws(',',id,name,remark) from t_kenyon;
   concat_ws   
---------------
 1,test,kenyon
 2,just,china
 3,iam,lovingU
 4,test
 5,adele
(5 rows)

postgres=# select concat_ws('_',id,name,remark) from t_kenyon;
   concat_ws   
---------------
 1_test_kenyon
 2_just_china
 3_iam_lovingU
 4_test
 5_adele
(5 rows)

postgres=# select concat_ws('',id,name,remark) from t_kenyon;
  concat_ws  
-------------
 1testkenyon
 2justchina
 3iamlovingU
 4test
 5adele
(5 rows)

postgres=# select concat_ws('^_*',id,name,remark) from t_kenyon;
     concat_ws     
-------------------
 1^_*test^_*kenyon
 2^_*just^_*china
 3^_*iam^_*lovingU
 4^_*test
 5^_*adele
(5 rows)

C. concat_ws Description concat function than a function of a separator function, in fact, concat upgraded version, if the separator is '', taken out and the result is the same concat. concat_ws Separator also supports multiple character as a delimiter, daily use was more likely to be ||.  


Second, cutting function 


1.split_part 


a. grammar Introduction

split_part(string text, delimiter text, field int)

Split string on delimiter and return the given field (counting from one)

b. Practical examples

postgres=# select split_part('abc~@~def~@~ghi','~@~', 2);
 split_part 
------------
 def
(1 row)

postgres=# select split_part('now|year|month','|',3);
 split_part 
------------
 month
(1 row)

c. described 
the function value by a specific location to pick up separators very effective 



2.regexp_split_to_table 


a. grammar Introduction

regexp_split_to_table(string text, pattern text [, flags text])

Split string using a POSIX regular expression as the delimiter.

b. Use Examples

postgres=# SELECT regexp_split_to_table('kenyon,love,,china,!',',');
 regexp_split_to_table 
-----------------------
 kenyon
 love
 
 china
 !
(5 rows)

--按分割符切割
postgres=# SELECT regexp_split_to_table('kenyon, china loves',E'\\s');
 regexp_split_to_table 
-----------------------
 kenyon,
 china
 loves
(3 rows)

--按字母切割
postgres=# SELECT regexp_split_to_table('kenyon,,china',E'\\s*');
 regexp_split_to_table 
-----------------------
 k
 e
 n
 y
 o
 n
 ,
 ,
 c
 h
 i
 n
 a
(13 rows)

 

3.regexp_split_to_array 


a. grammar Introduction

regexp_split_to_array(string text, pattern text [, flags text ])

Split string using a POSIX regular expression as the delimiter.

b. Practical examples

postgres=# SELECT regexp_split_to_array('kenyon,love,,china,!',',');
  regexp_split_to_array   
--------------------------
 {kenyon,love,"",china,!}
(1 row)

postgres=# SELECT regexp_split_to_array('kenyon,love,,china!','s*');
             regexp_split_to_array             
-----------------------------------------------
 {k,e,n,y,o,n,",",l,o,v,e,",",",",c,h,i,n,a,!}
(1 row)

c. described 
flag used above where s * indicates split all, E '\\ s' indicates that the escape space

 

 

Three: multi-line results are added

group_concat (field)

select group_concat(id) from xxxxxtable

Note: This function is the result of a default limit of 1024

Can be viewed by sql: show variables like 'group_concat_max_len';

By statement can temporarily change, but it resumed after the restart

SET GLOBAL group_concat_max_len = 4294967295;

SET SESSION group_concat_max_len = 4294967295;


 

Published 57 original articles · won praise 15 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_41694906/article/details/90751594