Y una combinación de corte de la base de datos SQL función

Tabla de contenido

Una combinación de la función 

1.concat 

2.concat_ws 

En segundo lugar, la función de corte 

1.split_part 

2.regexp_split_to_table 

3.regexp_split_to_array 

se añaden los resultados de varias líneas: tres


Una combinación de la función 


1.concat 


a. Introducción gramática

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 ejemplos prácticos:

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. Descripción 
función concat es puramente una función de empalme, el empalme puede ignorar el valor de un valor nulo, sin delimitador de mosaico, el delimitador si es necesario, es necesario utilizar los siguientes CONCAT_WS de función. 


2.concat_ws 


a. Introducción gramática

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. la aplicación práctica

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 función Descripción concat que una función de una función de separador, de hecho, la versión concat actualizado, si el separador es '', tomada a cabo y el resultado es el mismo concat. CONCAT_WS separador también es compatible con el carácter múltiple como un delimitador, el uso diario era más probable que sea ||.  


En segundo lugar, la función de corte 


1.split_part 


a. Introducción gramática

split_part(string text, delimiter text, field int)

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

b. Ejemplos prácticos

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. se describe 
el valor de la función de un lugar específico para recoger separadores muy eficaces 



2.regexp_split_to_table 


a. Introducción gramática

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

Split string using a POSIX regular expression as the delimiter.

b. Ejemplos de Uso

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. Introducción gramática

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

Split string using a POSIX regular expression as the delimiter.

b. Ejemplos prácticos

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. se describe 
bandera utilizada por encima de donde s * indica dividir todo, E '\\ s' indica que el espacio de escape

 

 

se añaden los resultados de varias líneas: tres

GROUP_CONCAT (campo)

seleccionar GROUP_CONCAT (id) de xxxxxtable

Nota: Esta función es el resultado de un límite predeterminado de 1024

Pueden ser vistos por SQL: Mostrar variables como 'group_concat_max_len';

Por declaración puede cambiar temporalmente, pero se reanudó después de la reanudación

SET GLOBAL group_concat_max_len = 4294967295;

SET SESSION group_concat_max_len = 4294967295;


 

Publicado 57 artículos originales · elogios ganado 15 · Vistas a 40000 +

Supongo que te gusta

Origin blog.csdn.net/qq_41694906/article/details/90751594
Recomendado
Clasificación