PostgreSQL中的数组与Any
建立表:
CREATE TABLE sal_emp ( name text, pay_by_quarter integer[], schedule text[][] );
插入数据:
INSERT INTO sal_emp VALUES ('Bill', ARRAY[10000, 11000, 9000, 13000], ARRAY[['meeting', 'lunch'], ['training', 'presentation']]); INSERT INTO sal_emp VALUES ('Carol', ARRAY[20000, 25000, 25000, 25000], ARRAY[['breakfast', 'consulting'], ['meeting', 'lunch']]); INSERT INTO sal_emp VALUES ('Frank', ARRAY[10000, 10000, 10000, 10000], ARRAY[['breakfast', 'training'], ['meeting', 'lunch']]);
查看:
pgsql=# SELECT * FROM sal_emp; name | pay_by_quarter | schedule -------+---------------------------+------------------------------------------- Bill | {10000,11000,9000,13000} | {{meeting,lunch},{training,presentation}} Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}} Frank | {10000,10000,10000,10000} | {{breakfast,training},{meeting,lunch}} (3 rows) pgsql=#
用Any来查询:
pgsql=# SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter); name | pay_by_quarter | schedule -------+---------------------------+------------------------------------------- Bill | {10000,11000,9000,13000} | {{meeting,lunch},{training,presentation}} Frank | {10000,10000,10000,10000} | {{breakfast,training},{meeting,lunch}} (2 rows) pgsql=#
用ALL来查询:
pgsql=# SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter); name | pay_by_quarter | schedule -------+---------------------------+---------------------------------------- Frank | {10000,10000,10000,10000} | {{breakfast,training},{meeting,lunch}} (1 row) pgsql=#
用下标来查询:
pgsql=# SELECT * FROM sal_emp WHERE 9000 = pay_by_quarter[3]; name | pay_by_quarter | schedule ------+--------------------------+------------------------------------------- Bill | {10000,11000,9000,13000} | {{meeting,lunch},{training,presentation}} (1 row) pgsql=#