PostgreSQL/Array/Array Column Select
Содержание
- 1 Accessing two dimensional Arrays
- 2 Array Input and Output Syntax
- 3 Reference array element
- 4 Searching in Arrays
- 5 SELECT ARRAY[1,2] || ARRAY3,4 AS array
- 6 Select array column by index
- 7 Selecting array values with subscripts
- 8 Selecting entire array values
- 9 Selecting From a Multi-Dimensional Array
- 10 Specify non-default array subscripts in an array literal
- 11 Use array constant in select statement
- 12 Using two dimension array
Accessing two dimensional Arrays
<source lang="sql">
postgres=# CREATE TABLE sal_emp ( postgres(# name text, postgres(# pay_by_quarter integer[], postgres(# schedule text[][] postgres(# ); CREATE TABLE postgres=# postgres=# INSERT INTO sal_emp postgres-# VALUES ("Bill", postgres(# ARRAY[10000, 10000, 10000, 10000], postgres(# ARRAY[["meeting", "lunch"], ["training", "presentation"]]); INSERT 0 1 postgres=# postgres=# INSERT INTO sal_emp postgres-# VALUES ("Carol", postgres(# ARRAY[20000, 25000, 25000, 25000], postgres(# ARRAY[["breakfast", "consulting"], ["meeting", "lunch"]]); INSERT 0 1 postgres=# postgres=# -- Accessing Arrays postgres=# postgres=# SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];
name
Carol
(1 row) postgres=# postgres=# SELECT * FROM sal_emp;
name | pay_by_quarter | schedule
+---------------------------+-------------------------------------------
Bill | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}} Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows) postgres=# postgres=# postgres=# drop table sal_emp; DROP TABLE postgres=# postgres=#
</source>
Array Input and Output Syntax
<source lang="sql">
postgres=# postgres=# -- Array Input and Output Syntax postgres=# SELECT 1 || ARRAY[2,3] AS array;
array
[0:2]={1,2,3}
(1 row) postgres=# postgres=#
</source>
Reference array element
<source lang="sql">
postgres=# postgres=# postgres=# CREATE TABLE sal_emp ( postgres(# name text, postgres(# pay_by_quarter integer[], postgres(# schedule text[][] postgres(# ); CREATE TABLE postgres=# postgres=# INSERT INTO sal_emp postgres-# VALUES ("Bill", postgres(# ARRAY[10000, 10000, 10000, 10000], postgres(# ARRAY[["meeting", "lunch"], ["training", "presentation"]]); INSERT 0 1 postgres=# postgres=# INSERT INTO sal_emp postgres-# VALUES ("Carol", postgres(# ARRAY[20000, 25000, 25000, 25000], postgres(# ARRAY[["breakfast", "consulting"], ["meeting", "lunch"]]); INSERT 0 1 postgres=# postgres=# -- Retrieves the third quarter pay of all employees: postgres=# postgres=# SELECT pay_by_quarter[3] FROM sal_emp;
pay_by_quarter
10000 25000
(2 rows) postgres=# postgres=# SELECT * FROM sal_emp;
name | pay_by_quarter | schedule
+---------------------------+-------------------------------------------
Bill | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}} Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows) postgres=# postgres=# drop table sal_emp; DROP TABLE postgres=# postgres=#
</source>
Searching in Arrays
<source lang="sql">
postgres=# postgres=# postgres=# CREATE TABLE sal_emp ( postgres(# name text, postgres(# pay_by_quarter integer[], postgres(# schedule text[][] postgres(# ); CREATE TABLE postgres=# postgres=# INSERT INTO sal_emp postgres-# VALUES ("Bill", postgres(# ARRAY[10000, 10000, 10000, 10000], postgres(# ARRAY[["meeting", "lunch"], ["training", "presentation"]]); INSERT 0 1 postgres=# postgres=# INSERT INTO sal_emp postgres-# VALUES ("Carol", postgres(# ARRAY[20000, 25000, 25000, 25000], postgres(# ARRAY[["breakfast", "consulting"], ["meeting", "lunch"]]); INSERT 0 1 postgres=# postgres=# SELECT * FROM sal_emp;
name | pay_by_quarter | schedule
+---------------------------+-------------------------------------------
Bill | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}} Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows) postgres=# -- Searching in Arrays postgres=# SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR postgres-# pay_by_quarter[2] = 10000 OR postgres-# pay_by_quarter[3] = 10000 OR postgres-# pay_by_quarter[4] = 10000;
name | pay_by_quarter | schedule
+---------------------------+-------------------------------------------
Bill | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
(1 row) postgres=# postgres=# postgres=# drop table sal_emp; DROP TABLE postgres=#
</source>
SELECT ARRAY[1,2] || ARRAY3,4 AS array
<source lang="sql">
postgres=# postgres=# SELECT ARRAY[1,2] || ARRAY3,4 AS array;
array
[0:1][1:2]={{1,2},{3,4}}
(1 row) postgres=#
</source>
Select array column by index
<source lang="sql">
postgres=# postgres=# postgres=# CREATE TABLE book (id integer,titles text[]); CREATE TABLE postgres=# postgres=# INSERT INTO book VALUES (102, "{"AAA\"s VVVV"}"); INSERT 0 1 postgres=# postgres=# INSERT INTO book VALUES (103, "{"AAA", "VVVV"}"); INSERT 0 1 postgres=# postgres=# postgres=# SELECT titles[2] FROM book;
titles
VVVV
(2 rows) postgres=# postgres=# SELECT titles[2] FROM book WHERE titles[2] IS NOT NULL;
titles
VVVV
(1 row) postgres=# postgres=# drop table book; DROP TABLE postgres=# postgres=#
</source>
Selecting array values with subscripts
<source lang="sql">
postgres=# postgres=# -- Selecting array values with subscripts postgres=# postgres=# CREATE TABLE book (id integer,titles text[]); CREATE TABLE postgres=# postgres=# INSERT INTO book VALUES (102, "{"AAA\"s VVVV"}"); INSERT 0 1 postgres=# postgres=# INSERT INTO book VALUES (103, "{"AAA", "VVVV"}"); INSERT 0 1 postgres=# postgres=# SELECT titles[1] FROM book;
titles
AAA"s VVVV AAA
(2 rows) postgres=# postgres=# drop table book; DROP TABLE postgres=# postgres=#
</source>
Selecting entire array values
<source lang="sql">
postgres=# postgres=# -- Selecting entire array values postgres=# postgres=# postgres=# CREATE TABLE book (id integer,titles text[]); CREATE TABLE postgres=# postgres=# INSERT INTO book VALUES (102, "{"AAA\"s VVVV"}"); INSERT 0 1 postgres=# postgres=# INSERT INTO book VALUES (103, "{"AAA", "VVVV"}"); INSERT 0 1 postgres=# postgres=# SELECT titles FROM book;
titles
{"AAA"s VVVV"} {AAA,VVVV}
(2 rows) postgres=# postgres=# drop table book; DROP TABLE postgres=# postgres=#
</source>
Selecting From a Multi-Dimensional Array
<source lang="sql">
postgres=# postgres=# CREATE TABLE authors (id integer,titles text[][]); CREATE TABLE postgres=# postgres=# INSERT INTO authors postgres-# VALUES (102, postgres(# "{{"J T", "T S"}, postgres"# {"C D", "G E"}, postgres"# {"A D", "A L"}}"); INSERT 0 1 postgres=# postgres=# -- Selecting From a Multi-Dimensional Array postgres=# postgres=# SELECT titles[1][1] AS author, titles[1][2] AS title FROM authors;
author | title
+-------
J T | T S
(1 row) postgres=# postgres=# drop table authors; DROP TABLE postgres=# postgres=#
</source>
Specify non-default array subscripts in an array literal
<source lang="sql">
postgres=# -- Specify non-default array subscripts in an array literal postgres=# postgres=# SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2 postgres-# FROM (SELECT "[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}"::int[] AS f1) AS ss;
e1 | e2
+----
1 | 6
(1 row) postgres=# postgres=#
</source>
Use array constant in select statement
<source lang="sql">
postgres=# postgres=# SELECT ARRAY[1,2,3+4];
array
{1,2,7}
(1 row)
</source>
Using two dimension array
<source lang="sql">
postgres=# postgres=# SELECT ARRAY[[1,2],[3,4]];
array
{{1,2},{3,4}}
(1 row) postgres=#
</source>