2017-05-25 42 views
0

在文檔頁面 https://www.postgresql.org/docs/9.6/static/functions-comparisons.html什麼是PostgreSQL ANY/SOME語法?

ANY/SOME描述如下: expression operator ANY (array expression)

給出的表格:

create table ints1(
    id int 
); 
create table arrays1(
    ints int[] 
); 

和查詢:

select id from ints1 where id = any (array[1, 2]); 

工作,如預期,因爲數組文字的語法是解釋性的這裏定義 https://www.postgresql.org/docs/9.6/static/arrays.html

select id from ints1 where id = any (select id from ints1 where id in (1, 2)); 

工作,但是爲什麼呢? 所以我想也許有1列的行智能型轉換設置爲數組類型,並試圖下面的查詢:

insert into arrays1 (ints) values (select id from ints1); 

不工作 - 附近選擇

語法錯誤,然後嘗試這樣:

insert into arrays1 (ints) values (ARRAY(select id from ints1)); 

這工作,並插入正確的值到arrays表中,但我沒有找到ARRAY(..)表達式應如何在文檔中工作。

我想,也許陣列(..)是某種類型轉換操作一遍,並試圖用::int[]

insert into arrays1 (ints) values ((select id from ints1)::int[]); 

錯誤:無法投類型整數到整數[]

  1. 任何人都可以指向array expression定義的文檔
  2. 澄清爲什麼第二個查詢運行正常,第三個失敗
  3. 說明如何ARRAY(..)表達的作品,什麼是與類型轉換爲int[]
+1

[數組構造函數](https://www.postgresql.org/docs/current/static/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS)和[Array Inp ut和輸出語法](https://www.postgresql.org/docs/current/static/arrays.html#ARRAYSIO)在手冊 –

+0

@a_horse_with_no_name中確實解釋了所有內容,但爲什麼第二個查詢可以工作 –

回答

1
+0

可能是一個有價值的資源,但只有鏈接的答案並不是真正的事情。如果這些是你所有的,或許一個評論會更合適。 –

+0

你可以給任何其他形式的鏈接嗎?我只能用'數組表達式'來找到表單,這樣整個話題纔會引起人們的興趣。通過第二個查詢,我的意思是查詢嵌套選擇'= any(select ...)' –

+0

@MikhailBoyarsky:這是貼出來的。 –