PostgreSQL
 SQL >> Datenbank >  >> RDS >> PostgreSQL

ANY vs. ALL: Einen PostgreSQL-Array-Vergleich korrigieren

Sie wollen ALL, nicht ANY. Aus dem Handbuch:

9.21.3. ANY/SOME (array)

expression operator ANY (array expression) 

[...] The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ANY is "true" if any true result is obtained.

Bei diesem Ausdruck:

1 != any(array[1,2]) 

erhalten wir true, denn (1 != 1) or (1 != 2) ist true. ANY ist im Grunde ein OR. Zum Beispiel:

=> select id from (values (1),(2),(3)) as t(id) where id != any(array[1,2]); id ---- 1 2 3 (3 rows) 

Nun zu ALL:

9.21.4. ALL (array)

expression operator ALL (array expression) 

[...] The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ALL is "true" if all comparisons yield true...

Bei diesem Ausdruck:

1 != all(array[1,2]) 

erhalten wir false, denn (1 != 1) and (1 != 2) ist false. ALL ist im Grunde ein AND. Zum Beispiel:

=> select id from (values (1),(2),(3)) as t(id) where id != all(array[1,2]); id ---- 3 (1 row) 

Um also jeden Wert eines Arrays auszuschließen, verwenden Sie ALL:

select "Ticket_id" from "Tickets" where "Status" = 1 and "Ticket_id" != all(array[1,2,3]) limit 6