sql >> Database teknologi >  >> RDS >> PostgreSQL

Postgresql - returner hele rækken som array

Måske dette:http://www.sqlfiddle.com/#!1/d41d8 /364

select translate(string_to_array(x.*::text,',')::text,'()','')::text[] 
from pg_tables as x
 

Sådan fungerer det (indefra og ud), 5 trin:

1.:

select x.*::text from pg_tables as x;
 

Eksempeloutput:

| X | ---------------------------------------------------------------- | (pg_catalog,pg_statistic,postgres,,t,f,f) | | (pg_catalog,pg_type,postgres,,t,f,f) |

2.:

select string_to_array(x.*::text,',') from pg_tables as x;
 

Eksempeloutput:

| STRING_TO_ARRAY | --------------------------------------------- | (pg_catalog,pg_statistic,postgres,,t,f,f) | | (pg_catalog,pg_type,postgres,,t,f,f) |

3.:

select string_to_array(x.*::text,',')::text from pg_tables as x;
 

Eksempeloutput:

| STRING_TO_ARRAY | ------------------------------------------------- | {(pg_catalog,pg_statistic,postgres,"",t,f,f)} | | {(pg_catalog,pg_type,postgres,"",t,f,f)} |

4.:

select translate( string_to_array(x.*::text,',')::text, '()', '') from pg_tables as x
 

Eksempeloutput:

| TRANSLATE | ----------------------------------------------- | {pg_catalog,pg_statistic,postgres,"",t,f,f} | | {pg_catalog,pg_type,postgres,"",t,f,f} |

Til sidst:

select translate( string_to_array(x.*::text,',')::text, '()', '')::text[] 
from pg_tables as x
 

Eksempeloutput:

| TRANSLATE | ------------------------------------------- | pg_catalog,pg_statistic,postgres,,t,f,f | | pg_catalog,pg_type,postgres,,t,f,f |

Live test:http://www.sqlfiddle.com/#!1/d41d8/ 373

For at bevise, at det virker:

with a as 
(
  select translate( string_to_array(x.*::text,',')::text, '()', '')::text[] as colArray 
  from pg_tables as x
)
select row_number() over(), unnest(colArray)
from a;
 

Eksempeloutput:

| ROW_NUMBER | UNNEST | ---------------------------------------- | 1 | pg_catalog | | 1 | pg_statistic | | 1 | postgres | | 1 | | | 1 | t | | 1 | f | | 1 | f | | 2 | pg_catalog | | 2 | pg_type | | 2 | postgres | | 2 | | | 2 | t | | 2 | f | | 2 | f |

  1. Hvordan kan jeg kopiere en enkelt række/post fra en MySQL-instans til en anden?

  2. JOIN vs. HVOR:Hvorfor udviser to forespørgsler, der opnår identiske resultater, en præstationsforskel på 3-4 størrelsesordener?

  3. Hvordan overvåger jeg min MySQL-replikeringsserver?

  4. Få OLD Value i MySQL Trigger EFTER opdateringserklæring