PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Postgresql - kembalikan seluruh baris sebagai array

Mungkin ini:http://www.sqlfiddle.com/#!1/d41d8 /364

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

Cara kerjanya (dalam-keluar), 5 langkah:

pertama:

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

Contoh Keluaran:

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

kedua:

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

Contoh Keluaran:

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

ketiga:

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

Contoh Keluaran:

|                               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

Contoh Keluaran:

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

Akhirnya:

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

Contoh Keluaran:

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

Tes langsung:http://www.sqlfiddle.com/#!1/d41d8/ 373

Untuk membuktikan bahwa itu berhasil:

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;

Contoh keluaran:

| 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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Tidak dapat menghubungkan Aplikasi Android Studio saya dengan PostgreSQL

  2. Menulis kolom JSON ke Postgres menggunakan Pandas .to_sql

  3. Kurangi Tahun dari Tanggal di PostgreSQL

  4. Optimalkan kueri GROUP BY untuk mengambil baris terbaru per pengguna

  5. Postgres pl/pgsql ERROR:kolom column_name tidak ada