PostgreSQL/Array/Array Column Select — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:14, 26 мая 2010
Содержание
- 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
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=#
Array Input and Output Syntax
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=#
Reference array element
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=#
Searching in Arrays
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=#
SELECT ARRAY[1,2] || ARRAY3,4 AS array
postgres=#
postgres=# SELECT ARRAY[1,2] || ARRAY[[3,4]] AS array;
array
--------------------------
[0:1][1:2]={{1,2},{3,4}}
(1 row)
postgres=#
Select array column by index
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=#
Selecting array values with subscripts
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=#
Selecting entire array values
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=#
Selecting From a Multi-Dimensional Array
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=#
Specify non-default array subscripts in an array literal
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=#
Use array constant in select statement
postgres=#
postgres=# SELECT ARRAY[1,2,3+4];
array
---------
{1,2,7}
(1 row)
Using two dimension array
postgres=#
postgres=# SELECT ARRAY[[1,2],[3,4]];
array
---------------
{{1,2},{3,4}}
(1 row)
postgres=#